clang  12.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 the diagnostic message with the error message associated
30 /// and the information on the location of the problem.
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  /// Fixes for this diagnostic, grouped by file path.
47  llvm::StringMap<Replacements> Fix;
48 };
49 
50 /// Represents a range within a specific source file.
51 struct FileByteRange {
52  FileByteRange() = default;
53 
55 
56  std::string FilePath;
57  unsigned FileOffset;
58  unsigned Length;
59 };
60 
61 /// Represents the diagnostic with the level of severity and possible
62 /// fixes to be applied.
63 struct Diagnostic {
64  enum Level {
67  };
68 
69  Diagnostic() = default;
70 
71  Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel,
72  StringRef BuildDirectory);
73 
74  Diagnostic(llvm::StringRef DiagnosticName, const DiagnosticMessage &Message,
75  const SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel,
76  llvm::StringRef BuildDirectory,
77  const SmallVector<FileByteRange, 1> &Ranges);
78 
79  /// Name identifying the Diagnostic.
80  std::string DiagnosticName;
81 
82  /// Message associated to the diagnostic.
84 
85  /// Potential notes about the diagnostic.
87 
88  /// Diagnostic level. Can indicate either an error or a warning.
90 
91  /// A build directory of the diagnostic source file.
92  ///
93  /// It's an absolute path which is `directory` field of the source file in
94  /// compilation database. If users don't specify the compilation database
95  /// directory, it is the current directory where clang-tidy runs.
96  ///
97  /// Note: it is empty in unittest.
98  std::string BuildDirectory;
99 
100  /// Extra source ranges associated with the diagnostic (in addition to the
101  /// location of the Message above).
103 };
104 
105 /// Collection of Diagnostics generated from a single translation unit.
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
DiagnosticMessage(llvm::StringRef Message="")
Definition: Diagnostic.cpp:21
const llvm::StringMap< Replacements > * selectFirstFix(const Diagnostic &D)
Get the first fix to apply for this diagnostic.
Definition: Diagnostic.cpp:61
Represents a range within a specific source file.
Definition: Diagnostic.h:51
Level DiagLevel
Diagnostic level. Can indicate either an error or a warning.
Definition: Diagnostic.h:89
Represents the diagnostic with the level of severity and possible fixes to be applied.
Definition: Diagnostic.h:63
std::string BuildDirectory
A build directory of the diagnostic source file.
Definition: Diagnostic.h:98
llvm::Error Error
Defines the Diagnostic-related interfaces.
llvm::StringMap< Replacements > Fix
Fixes for this diagnostic, grouped by file path.
Definition: Diagnostic.h:47
std::string DiagnosticName
Name identifying the Diagnostic.
Definition: Diagnostic.h:80
std::string MainSourceFile
Name of the main source for the translation unit.
Definition: Diagnostic.h:108
SmallVector< FileByteRange, 1 > Ranges
Extra source ranges associated with the diagnostic (in addition to the location of the Message above)...
Definition: Diagnostic.h:102
A source range independent of the SourceManager.
Definition: Replacement.h:44
Collection of Diagnostics generated from a single translation unit.
Definition: Diagnostic.h:106
Represents a character-granular source range.
SmallVector< DiagnosticMessage, 1 > Notes
Potential notes about the diagnostic.
Definition: Diagnostic.h:86
Represents the diagnostic message with the error message associated and the information on the locati...
Definition: Diagnostic.h:31
Encodes a location in the source.
DiagnosticMessage Message
Message associated to the diagnostic.
Definition: Diagnostic.h:83
Dataflow Directional Tag Classes.
std::vector< Diagnostic > Diagnostics
Definition: Diagnostic.h:109
This class handles loading and caching of source files into memory.