clang  14.0.0git
Diagnostic.cpp
Go to the documentation of this file.
1 //===--- Diagnostic.cpp - Framework for clang diagnostics tools ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements classes to support/store diagnostics refactoring.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "llvm/ADT/STLExtras.h"
17 
18 namespace clang {
19 namespace tooling {
20 
21 DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message)
22  : Message(Message), FileOffset(0) {}
23 
24 DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message,
25  const SourceManager &Sources,
26  SourceLocation Loc)
27  : Message(Message), FileOffset(0) {
28  assert(Loc.isValid() && Loc.isFileID());
29  FilePath = std::string(Sources.getFilename(Loc));
30 
31  // Don't store offset in the scratch space. It doesn't tell anything to the
32  // user. Moreover, it depends on the history of macro expansions and thus
33  // prevents deduplication of warnings in headers.
34  if (!FilePath.empty())
35  FileOffset = Sources.getFileOffset(Loc);
36 }
37 
39  const SourceManager &Sources, CharSourceRange Range)
40  : FileOffset(0), Length(0) {
41  FilePath = std::string(Sources.getFilename(Range.getBegin()));
42  if (!FilePath.empty()) {
43  FileOffset = Sources.getFileOffset(Range.getBegin());
44  Length = Sources.getFileOffset(Range.getEnd()) - FileOffset;
45  }
46 }
47 
48 Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
49  Diagnostic::Level DiagLevel, StringRef BuildDirectory)
50  : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
51  BuildDirectory(BuildDirectory) {}
52 
53 Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
54  const DiagnosticMessage &Message,
56  Level DiagLevel, llvm::StringRef BuildDirectory)
57  : DiagnosticName(DiagnosticName), Message(Message), Notes(Notes),
58  DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
59 
60 const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D) {
61  if (!D.Message.Fix.empty())
62  return &D.Message.Fix;
63  auto Iter = llvm::find_if(D.Notes, [](const tooling::DiagnosticMessage &D) {
64  return !D.Fix.empty();
65  });
66  if (Iter != D.Notes.end())
67  return &Iter->Fix;
68  return nullptr;
69 }
70 
71 } // end namespace tooling
72 } // end namespace clang
clang::tooling::Diagnostic
Represents the diagnostic with the level of severity and possible fixes to be applied.
Definition: Diagnostic.h:68
clang::SourceManager::getFilename
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Definition: SourceManager.cpp:1018
clang::tooling::Diagnostic::Message
DiagnosticMessage Message
Message associated to the diagnostic.
Definition: Diagnostic.h:88
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::tooling::DiagnosticMessage::DiagnosticMessage
DiagnosticMessage(llvm::StringRef Message="")
Definition: Diagnostic.cpp:21
clang::SourceManager::getFileOffset
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
Definition: SourceManager.h:1287
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::tooling::Range
A source range independent of the SourceManager.
Definition: Replacement.h:44
SourceManager.h
clang::tooling::DiagnosticMessage::FilePath
std::string FilePath
Definition: Diagnostic.h:55
Diagnostic.h
clang::tooling::DiagnosticMessage::Fix
llvm::StringMap< Replacements > Fix
Fixes for this diagnostic, grouped by file path.
Definition: Diagnostic.h:59
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::tooling::FileByteRange::FileOffset
unsigned FileOffset
Definition: Diagnostic.h:36
clang::tooling::FileByteRange::FilePath
std::string FilePath
Definition: Diagnostic.h:35
clang::SourceLocation::isFileID
bool isFileID() const
Definition: SourceLocation.h:104
clang::tooling::DiagnosticMessage::FileOffset
unsigned FileOffset
Definition: Diagnostic.h:56
SourceLocation.h
clang::tooling::selectFirstFix
const llvm::StringMap< Replacements > * selectFirstFix(const Diagnostic &D)
Get the first fix to apply for this diagnostic.
Definition: Diagnostic.cpp:60
clang::tooling::Diagnostic::Level
Level
Definition: Diagnostic.h:69
clang::tooling::Diagnostic::Notes
SmallVector< DiagnosticMessage, 1 > Notes
Potential notes about the diagnostic.
Definition: Diagnostic.h:91
clang::CharSourceRange
Represents a character-granular source range.
Definition: SourceLocation.h:255
clang
Definition: CalledOnceCheck.h:17
clang::tooling::FileByteRange::Length
unsigned Length
Definition: Diagnostic.h:37
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:112
clang::tooling::FileByteRange::FileByteRange
FileByteRange()=default
clang::tooling::DiagnosticMessage
Represents the diagnostic message with the error message associated and the information on the locati...
Definition: Diagnostic.h:42
clang::tooling::Diagnostic::Diagnostic
Diagnostic()=default