clang  14.0.0git
Diagnostic.h
Go to the documentation of this file.
1 //===--- Diagnostic.h - Framework for clang diagnostics tools --*- C++ -*-===//
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 // \file
10 // Structures supporting diagnostics and refactorings that span multiple
11 // translation units. Indicate diagnostics reports and replacements
12 // suggestions for the analyzed sources.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
17 #define LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
18 
19 #include "Replacement.h"
20 #include "clang/Basic/Diagnostic.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include <string>
25 
26 namespace clang {
27 namespace tooling {
28 
29 /// Represents a range within a specific source file.
30 struct FileByteRange {
31  FileByteRange() = default;
32 
34 
36  unsigned FileOffset;
37  unsigned Length;
38 };
39 
40 /// Represents the diagnostic message with the error message associated
41 /// and the information on the location of the problem.
43  DiagnosticMessage(llvm::StringRef Message = "");
44 
45  /// Constructs a diagnostic message with anoffset to the diagnostic
46  /// within the file where the problem occurred.
47  ///
48  /// \param Loc Should be a file location, it is not meaningful for a macro
49  /// location.
50  ///
51  DiagnosticMessage(llvm::StringRef Message, const SourceManager &Sources,
52  SourceLocation Loc);
53 
56  unsigned FileOffset;
57 
58  /// Fixes for this diagnostic, grouped by file path.
59  llvm::StringMap<Replacements> Fix;
60 
61  /// Extra source ranges associated with the note, in addition to the location
62  /// of the Message itself.
64 };
65 
66 /// Represents the diagnostic with the level of severity and possible
67 /// fixes to be applied.
68 struct Diagnostic {
69  enum Level {
73  };
74 
75  Diagnostic() = default;
76 
77  Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel,
78  StringRef BuildDirectory);
79 
80  Diagnostic(llvm::StringRef DiagnosticName, const DiagnosticMessage &Message,
82  llvm::StringRef BuildDirectory);
83 
84  /// Name identifying the Diagnostic.
86 
87  /// Message associated to the diagnostic.
89 
90  /// Potential notes about the diagnostic.
92 
93  /// Diagnostic level. Can indicate either an error or a warning.
95 
96  /// A build directory of the diagnostic source file.
97  ///
98  /// It's an absolute path which is `directory` field of the source file in
99  /// compilation database. If users don't specify the compilation database
100  /// directory, it is the current directory where clang-tidy runs.
101  ///
102  /// Note: it is empty in unittest.
104 };
105 
106 /// Collection of Diagnostics generated from a single translation unit.
108  /// Name of the main source for the translation unit.
110  std::vector<Diagnostic> Diagnostics;
111 };
112 
113 /// Get the first fix to apply for this diagnostic.
114 /// \returns nullptr if no fixes are attached to the diagnostic.
115 const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D);
116 
117 } // end namespace tooling
118 } // end namespace clang
119 #endif // LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
clang::tooling::Diagnostic
Represents the diagnostic with the level of severity and possible fixes to be applied.
Definition: Diagnostic.h:68
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::tooling::DiagnosticMessage::Message
std::string Message
Definition: Diagnostic.h:54
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::tooling::Diagnostic::DiagnosticName
std::string DiagnosticName
Name identifying the Diagnostic.
Definition: Diagnostic.h:85
clang::tooling::Range
A source range independent of the SourceManager.
Definition: Replacement.h:44
clang::tooling::DiagnosticMessage::FilePath
std::string FilePath
Definition: Diagnostic.h:55
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::Diagnostic::BuildDirectory
std::string BuildDirectory
A build directory of the diagnostic source file.
Definition: Diagnostic.h:103
clang::tooling::FileByteRange::FileOffset
unsigned FileOffset
Definition: Diagnostic.h:36
clang::tooling::TranslationUnitDiagnostics::MainSourceFile
std::string MainSourceFile
Name of the main source for the translation unit.
Definition: Diagnostic.h:109
clang::tooling::FileByteRange::FilePath
std::string FilePath
Definition: Diagnostic.h:35
clang::DiagnosticsEngine::Remark
@ Remark
Definition: Diagnostic.h:197
clang::tooling::Diagnostic::Warning
@ Warning
Definition: Diagnostic.h:71
Diagnostic.h
clang::DiagnosticsEngine::Error
@ Error
Definition: Diagnostic.h:199
clang::tooling::DiagnosticMessage::Ranges
llvm::SmallVector< FileByteRange, 1 > Ranges
Extra source ranges associated with the note, in addition to the location of the Message itself.
Definition: Diagnostic.h:63
clang::tooling::DiagnosticMessage::FileOffset
unsigned FileOffset
Definition: Diagnostic.h:56
clang::tooling::Diagnostic::DiagLevel
Level DiagLevel
Diagnostic level. Can indicate either an error or a warning.
Definition: Diagnostic.h:94
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
Replacement.h
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::tooling::TranslationUnitDiagnostics::Diagnostics
std::vector< Diagnostic > Diagnostics
Definition: Diagnostic.h:110
clang::tooling::FileByteRange::FileByteRange
FileByteRange()=default
clang::tooling::TranslationUnitDiagnostics
Collection of Diagnostics generated from a single translation unit.
Definition: Diagnostic.h:107
clang::tooling::FileByteRange
Represents a range within a specific source file.
Definition: Diagnostic.h:30
clang::tooling::DiagnosticMessage
Represents the diagnostic message with the error message associated and the information on the locati...
Definition: Diagnostic.h:42
clang::DiagnosticsEngine::Warning
@ Warning
Definition: Diagnostic.h:198
clang::tooling::Diagnostic::Error
@ Error
Definition: Diagnostic.h:72
clang::tooling::Diagnostic::Remark
@ Remark
Definition: Diagnostic.h:70
clang::tooling::Diagnostic::Diagnostic
Diagnostic()=default