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 Remark = DiagnosticsEngine::Remark, 71 Warning = DiagnosticsEngine::Warning, 72 Error = DiagnosticsEngine::Error 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, 81 const SmallVector<DiagnosticMessage, 1> &Notes, Level DiagLevel, 82 llvm::StringRef BuildDirectory); 83 84 /// Name identifying the Diagnostic. 85 std::string DiagnosticName; 86 87 /// Message associated to the diagnostic. 88 DiagnosticMessage Message; 89 90 /// Potential notes about the diagnostic. 91 SmallVector<DiagnosticMessage, 1> Notes; 92 93 /// Diagnostic level. Can indicate either an error or a warning. 94 Level DiagLevel; 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. 107 struct TranslationUnitDiagnostics { 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 120