clang  19.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 
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.
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,
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.
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.
85  std::string DiagnosticName;
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.
103  std::string BuildDirectory;
104 };
105 
106 /// Collection of Diagnostics generated from a single translation unit.
108  /// Name of the main source for the translation unit.
109  std::string MainSourceFile;
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
Defines the Diagnostic-related interfaces.
SourceLocation Loc
Definition: SemaObjC.cpp:755
Represents a character-granular source range.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
A source range independent of the SourceManager.
Definition: Replacement.h:44
const llvm::StringMap< Replacements > * selectFirstFix(const Diagnostic &D)
Get the first fix to apply for this diagnostic.
Definition: Diagnostic.cpp:60
The JSON file list parser is used to communicate input to InstallAPI.
Represents the diagnostic message with the error message associated and the information on the locati...
Definition: Diagnostic.h:42
DiagnosticMessage(llvm::StringRef Message="")
Definition: Diagnostic.cpp:21
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
llvm::StringMap< Replacements > Fix
Fixes for this diagnostic, grouped by file path.
Definition: Diagnostic.h:59
Represents the diagnostic with the level of severity and possible fixes to be applied.
Definition: Diagnostic.h:68
std::string BuildDirectory
A build directory of the diagnostic source file.
Definition: Diagnostic.h:103
std::string DiagnosticName
Name identifying the Diagnostic.
Definition: Diagnostic.h:85
DiagnosticMessage Message
Message associated to the diagnostic.
Definition: Diagnostic.h:88
SmallVector< DiagnosticMessage, 1 > Notes
Potential notes about the diagnostic.
Definition: Diagnostic.h:91
Level DiagLevel
Diagnostic level. Can indicate either an error or a warning.
Definition: Diagnostic.h:94
Represents a range within a specific source file.
Definition: Diagnostic.h:30
Collection of Diagnostics generated from a single translation unit.
Definition: Diagnostic.h:107
std::vector< Diagnostic > Diagnostics
Definition: Diagnostic.h:110
std::string MainSourceFile
Name of the main source for the translation unit.
Definition: Diagnostic.h:109