clang 10.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.
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 /// Fixes for this diagnostic, grouped by file path.
47 llvm::StringMap<Replacements> Fix;
48 };
49
50 /// Represents the diagnostic with the level of severity and possible
51 /// fixes to be applied.
52 struct Diagnostic {
53 enum Level {
54 Warning = DiagnosticsEngine::Warning,
55 Error = DiagnosticsEngine::Error
56 };
57
58 Diagnostic() = default;
59
60 Diagnostic(llvm::StringRef DiagnosticName, Level DiagLevel,
61 StringRef BuildDirectory);
62
63 Diagnostic(llvm::StringRef DiagnosticName, const DiagnosticMessage &Message,
64 const SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel,
65 llvm::StringRef BuildDirectory);
66
67 /// Name identifying the Diagnostic.
68 std::string DiagnosticName;
69
70 /// Message associated to the diagnostic.
71 DiagnosticMessage Message;
72
73 /// Potential notes about the diagnostic.
74 SmallVector<DiagnosticMessage, 1> Notes;
75
76 /// Diagnostic level. Can indicate either an error or a warning.
77 Level DiagLevel;
78
79 /// A build directory of the diagnostic source file.
80 ///
81 /// It's an absolute path which is `directory` field of the source file in
82 /// compilation database. If users don't specify the compilation database
83 /// directory, it is the current directory where clang-tidy runs.
84 ///
85 /// Note: it is empty in unittest.
86 std::string BuildDirectory;
87 };
88
89 /// Collection of Diagnostics generated from a single translation unit.
90 struct TranslationUnitDiagnostics {
91 /// Name of the main source for the translation unit.
92 std::string MainSourceFile;
93 std::vector<Diagnostic> Diagnostics;
94 };
95
96 /// Get the first fix to apply for this diagnostic.
97 /// \returns nullptr if no fixes are attached to the diagnostic.
98 const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D);
99
100 } // end namespace tooling
101 } // end namespace clang
102 #endif // LLVM_CLANG_TOOLING_CORE_DIAGNOSTIC_H
clang::tooling::DiagnosticMessage::DiagnosticMessage
DiagnosticMessage(llvm::StringRef Message="")
Definition: Diagnostic.cpp:20
clang::tooling::selectFirstFix
const llvm::StringMap< Replacements > * selectFirstFix(const Diagnostic &D)
Get the first fix to apply for this diagnostic.
Definition: Diagnostic.cpp:49
clang::tooling::Diagnostic::DiagLevel
Level DiagLevel
Diagnostic level. Can indicate either an error or a warning.
Definition: Diagnostic.h:77
clang::tooling::DiagnosticMessage::Message
std::string Message
Definition: Diagnostic.h:42
clang::tooling::Diagnostic
Represents the diagnostic with the level of severity and possible fixes to be applied.
Definition: Diagnostic.h:52
clang::tooling::DiagnosticMessage::FilePath
std::string FilePath
Definition: Diagnostic.h:43
clang::tooling::Diagnostic::BuildDirectory
std::string BuildDirectory
A build directory of the diagnostic source file.
Definition: Diagnostic.h:86
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:19
Diagnostic.h
Defines the Diagnostic-related interfaces.
clang::tooling::DiagnosticMessage::Fix
llvm::StringMap< Replacements > Fix
Fixes for this diagnostic, grouped by file path.
Definition: Diagnostic.h:47
clang::tooling::Diagnostic::DiagnosticName
std::string DiagnosticName
Name identifying the Diagnostic.
Definition: Diagnostic.h:68
clang::tooling::TranslationUnitDiagnostics::MainSourceFile
std::string MainSourceFile
Name of the main source for the translation unit.
Definition: Diagnostic.h:92
clang::tooling::Diagnostic::Level
Level
Definition: Diagnostic.h:53
clang::tooling::DiagnosticMessage::FileOffset
unsigned FileOffset
Definition: Diagnostic.h:44
clang::tooling::TranslationUnitDiagnostics
Collection of Diagnostics generated from a single translation unit.
Definition: Diagnostic.h:90
clang::tooling::Diagnostic::Notes
SmallVector< DiagnosticMessage, 1 > Notes
Potential notes about the diagnostic.
Definition: Diagnostic.h:74
clang::tooling::DiagnosticMessage
Represents the diagnostic message with the error message associated and the information on the locati...
Definition: Diagnostic.h:31
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
llvm::SmallVector
Definition: LLVM.h:35
clang::tooling::Diagnostic::Message
DiagnosticMessage Message
Message associated to the diagnostic.
Definition: Diagnostic.h:71
Replacement.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::DiagnosticsEngine::Warning
Definition: Diagnostic.h:156
clang::DiagnosticsEngine::Error
Definition: Diagnostic.h:157
clang::tooling::TranslationUnitDiagnostics::Diagnostics
std::vector< Diagnostic > Diagnostics
Definition: Diagnostic.h:93
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:596