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 the diagnostic message with the error message associated |
30 | /// and the information on the location of the problem. |
31 | struct DiagnosticMessage { |
32 | DiagnosticMessage(llvm::StringRef Message = ""); |
33 | |
34 | /// Constructs a diagnostic message with anoffset to the diagnostic |
35 | /// within the file where the problem occurred. |
36 | /// |
37 | /// \param Loc Should be a file location, it is not meaningful for a macro |
38 | /// location. |
39 | /// |
40 | DiagnosticMessage(llvm::StringRef Message, const SourceManager &Sources, |
41 | SourceLocation Loc); |
42 | std::string Message; |
43 | std::string FilePath; |
44 | unsigned FileOffset; |
45 | }; |
46 | |
47 | /// Represents the diagnostic with the level of severity and possible |
48 | /// fixes to be applied. |
49 | struct Diagnostic { |
50 | enum Level { |
51 | Warning = DiagnosticsEngine::Warning, |
52 | Error = DiagnosticsEngine::Error |
53 | }; |
54 | |
55 | Diagnostic() = default; |
56 | |
57 | Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel, |
58 | StringRef BuildDirectory); |
59 | |
60 | Diagnostic(llvm::StringRef DiagnosticName, const DiagnosticMessage &Message, |
61 | const llvm::StringMap<Replacements> &Fix, |
62 | const SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel, |
63 | llvm::StringRef BuildDirectory); |
64 | |
65 | /// Name identifying the Diagnostic. |
66 | std::string DiagnosticName; |
67 | |
68 | /// Message associated to the diagnostic. |
69 | DiagnosticMessage Message; |
70 | |
71 | /// Fixes to apply, grouped by file path. |
72 | llvm::StringMap<Replacements> Fix; |
73 | |
74 | /// Potential notes about the diagnostic. |
75 | SmallVector<DiagnosticMessage, 1> Notes; |
76 | |
77 | /// Diagnostic level. Can indicate either an error or a warning. |
78 | Level DiagLevel; |
79 | |
80 | /// A build directory of the diagnostic source file. |
81 | /// |
82 | /// It's an absolute path which is `directory` field of the source file in |
83 | /// compilation database. If users don't specify the compilation database |
84 | /// directory, it is the current directory where clang-tidy runs. |
85 | /// |
86 | /// Note: it is empty in unittest. |
87 | std::string BuildDirectory; |
88 | }; |
89 | |
90 | /// Collection of Diagnostics generated from a single translation unit. |
91 | struct TranslationUnitDiagnostics { |
92 | /// Name of the main source for the translation unit. |
93 | std::string MainSourceFile; |
94 | std::vector<Diagnostic> Diagnostics; |
95 | }; |
96 | |
97 | } // end namespace tooling |
98 | } // end namespace clang |
99 | #endif // LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H |
100 |