/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Core/Diagnostic.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- Diagnostic.cpp - Framework for clang diagnostics tools ----------===// |
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 | | // Implements classes to support/store diagnostics refactoring. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Tooling/Core/Diagnostic.h" |
14 | | #include "clang/Basic/SourceLocation.h" |
15 | | #include "clang/Basic/SourceManager.h" |
16 | | #include "llvm/ADT/STLExtras.h" |
17 | | |
18 | | namespace clang { |
19 | | namespace tooling { |
20 | | |
21 | | DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message) |
22 | 16 | : Message(Message), FileOffset(0) {} |
23 | | |
24 | | DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message, |
25 | | const SourceManager &Sources, |
26 | | SourceLocation Loc) |
27 | 0 | : Message(Message), FileOffset(0) { |
28 | 0 | assert(Loc.isValid() && Loc.isFileID()); |
29 | 0 | FilePath = std::string(Sources.getFilename(Loc)); |
30 | | |
31 | | // Don't store offset in the scratch space. It doesn't tell anything to the |
32 | | // user. Moreover, it depends on the history of macro expansions and thus |
33 | | // prevents deduplication of warnings in headers. |
34 | 0 | if (!FilePath.empty()) |
35 | 0 | FileOffset = Sources.getFileOffset(Loc); |
36 | 0 | } |
37 | | |
38 | | FileByteRange::FileByteRange( |
39 | | const SourceManager &Sources, CharSourceRange Range) |
40 | 0 | : FileOffset(0), Length(0) { |
41 | 0 | FilePath = std::string(Sources.getFilename(Range.getBegin())); |
42 | 0 | if (!FilePath.empty()) { |
43 | 0 | FileOffset = Sources.getFileOffset(Range.getBegin()); |
44 | 0 | Length = Sources.getFileOffset(Range.getEnd()) - FileOffset; |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | | Diagnostic::Diagnostic(llvm::StringRef DiagnosticName, |
49 | | Diagnostic::Level DiagLevel, StringRef BuildDirectory) |
50 | 0 | : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel), |
51 | 0 | BuildDirectory(BuildDirectory) {} |
52 | | |
53 | | Diagnostic::Diagnostic(llvm::StringRef DiagnosticName, |
54 | | const DiagnosticMessage &Message, |
55 | | const SmallVector<DiagnosticMessage, 1> &Notes, |
56 | | Level DiagLevel, llvm::StringRef BuildDirectory) |
57 | 8 | : DiagnosticName(DiagnosticName), Message(Message), Notes(Notes), |
58 | 8 | DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {} |
59 | | |
60 | 0 | const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D) { |
61 | 0 | if (!D.Message.Fix.empty()) |
62 | 0 | return &D.Message.Fix; |
63 | 0 | auto Iter = llvm::find_if(D.Notes, [](const tooling::DiagnosticMessage &D) { |
64 | 0 | return !D.Fix.empty(); |
65 | 0 | }); |
66 | 0 | if (Iter != D.Notes.end()) |
67 | 0 | return &Iter->Fix; |
68 | 0 | return nullptr; |
69 | 0 | } |
70 | | |
71 | | } // end namespace tooling |
72 | | } // end namespace clang |