xref: /netbsd-src/external/apache2/llvm/dist/clang/include/clang/Tooling/Core/Diagnostic.h (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
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 
33   FileByteRange(const SourceManager &Sources, CharSourceRange Range);
34 
35   std::string FilePath;
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.
42 struct DiagnosticMessage {
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 
54   std::string Message;
55   std::string FilePath;
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.
63   llvm::SmallVector<FileByteRange, 1> Ranges;
64 };
65 
66 /// Represents the diagnostic with the level of severity and possible
67 /// fixes to be applied.
68 struct Diagnostic {
69   enum Level {
70     Warning = DiagnosticsEngine::Warning,
71     Error = DiagnosticsEngine::Error
72   };
73 
74   Diagnostic() = default;
75 
76   Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel,
77              StringRef BuildDirectory);
78 
79   Diagnostic(llvm::StringRef DiagnosticName, const DiagnosticMessage &Message,
80              const SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel,
81              llvm::StringRef BuildDirectory);
82 
83   /// Name identifying the Diagnostic.
84   std::string DiagnosticName;
85 
86   /// Message associated to the diagnostic.
87   DiagnosticMessage Message;
88 
89   /// Potential notes about the diagnostic.
90   SmallVector<DiagnosticMessage, 1> Notes;
91 
92   /// Diagnostic level. Can indicate either an error or a warning.
93   Level DiagLevel;
94 
95   /// A build directory of the diagnostic source file.
96   ///
97   /// It's an absolute path which is `directory` field of the source file in
98   /// compilation database. If users don't specify the compilation database
99   /// directory, it is the current directory where clang-tidy runs.
100   ///
101   /// Note: it is empty in unittest.
102   std::string BuildDirectory;
103 };
104 
105 /// Collection of Diagnostics generated from a single translation unit.
106 struct TranslationUnitDiagnostics {
107   /// Name of the main source for the translation unit.
108   std::string MainSourceFile;
109   std::vector<Diagnostic> Diagnostics;
110 };
111 
112 /// Get the first fix to apply for this diagnostic.
113 /// \returns nullptr if no fixes are attached to the diagnostic.
114 const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D);
115 
116 } // end namespace tooling
117 } // end namespace clang
118 #endif // LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
119