xref: /openbsd-src/gnu/llvm/clang/lib/Tooling/Core/Diagnostic.cpp (revision a9ac8606c53d55cee9c3a39778b249c51df111ef)
1e5dd7070Spatrick //===--- Diagnostic.cpp - Framework for clang diagnostics tools ----------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  Implements classes to support/store diagnostics refactoring.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick 
13e5dd7070Spatrick #include "clang/Tooling/Core/Diagnostic.h"
14ec727ea7Spatrick #include "clang/Basic/SourceLocation.h"
15e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
16e5dd7070Spatrick #include "llvm/ADT/STLExtras.h"
17e5dd7070Spatrick 
18e5dd7070Spatrick namespace clang {
19e5dd7070Spatrick namespace tooling {
20e5dd7070Spatrick 
DiagnosticMessage(llvm::StringRef Message)21e5dd7070Spatrick DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message)
22e5dd7070Spatrick     : Message(Message), FileOffset(0) {}
23e5dd7070Spatrick 
DiagnosticMessage(llvm::StringRef Message,const SourceManager & Sources,SourceLocation Loc)24e5dd7070Spatrick DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message,
25e5dd7070Spatrick                                      const SourceManager &Sources,
26e5dd7070Spatrick                                      SourceLocation Loc)
27e5dd7070Spatrick     : Message(Message), FileOffset(0) {
28e5dd7070Spatrick   assert(Loc.isValid() && Loc.isFileID());
29ec727ea7Spatrick   FilePath = std::string(Sources.getFilename(Loc));
30e5dd7070Spatrick 
31e5dd7070Spatrick   // Don't store offset in the scratch space. It doesn't tell anything to the
32e5dd7070Spatrick   // user. Moreover, it depends on the history of macro expansions and thus
33e5dd7070Spatrick   // prevents deduplication of warnings in headers.
34e5dd7070Spatrick   if (!FilePath.empty())
35e5dd7070Spatrick     FileOffset = Sources.getFileOffset(Loc);
36e5dd7070Spatrick }
37e5dd7070Spatrick 
FileByteRange(const SourceManager & Sources,CharSourceRange Range)38ec727ea7Spatrick FileByteRange::FileByteRange(
39ec727ea7Spatrick     const SourceManager &Sources, CharSourceRange Range)
40ec727ea7Spatrick     : FileOffset(0), Length(0) {
41ec727ea7Spatrick   FilePath = std::string(Sources.getFilename(Range.getBegin()));
42ec727ea7Spatrick   if (!FilePath.empty()) {
43ec727ea7Spatrick     FileOffset = Sources.getFileOffset(Range.getBegin());
44ec727ea7Spatrick     Length = Sources.getFileOffset(Range.getEnd()) - FileOffset;
45ec727ea7Spatrick   }
46ec727ea7Spatrick }
47ec727ea7Spatrick 
Diagnostic(llvm::StringRef DiagnosticName,Diagnostic::Level DiagLevel,StringRef BuildDirectory)48e5dd7070Spatrick Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
49e5dd7070Spatrick                        Diagnostic::Level DiagLevel, StringRef BuildDirectory)
50e5dd7070Spatrick     : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
51e5dd7070Spatrick       BuildDirectory(BuildDirectory) {}
52e5dd7070Spatrick 
Diagnostic(llvm::StringRef DiagnosticName,const DiagnosticMessage & Message,const SmallVector<DiagnosticMessage,1> & Notes,Level DiagLevel,llvm::StringRef BuildDirectory)53e5dd7070Spatrick Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
54e5dd7070Spatrick                        const DiagnosticMessage &Message,
55e5dd7070Spatrick                        const SmallVector<DiagnosticMessage, 1> &Notes,
56*a9ac8606Spatrick                        Level DiagLevel, llvm::StringRef BuildDirectory)
57e5dd7070Spatrick     : DiagnosticName(DiagnosticName), Message(Message), Notes(Notes),
58*a9ac8606Spatrick       DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
59e5dd7070Spatrick 
selectFirstFix(const Diagnostic & D)60e5dd7070Spatrick const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D) {
61e5dd7070Spatrick    if (!D.Message.Fix.empty())
62e5dd7070Spatrick     return &D.Message.Fix;
63e5dd7070Spatrick   auto Iter = llvm::find_if(D.Notes, [](const tooling::DiagnosticMessage &D) {
64e5dd7070Spatrick     return !D.Fix.empty();
65e5dd7070Spatrick   });
66e5dd7070Spatrick   if (Iter != D.Notes.end())
67e5dd7070Spatrick     return &Iter->Fix;
68e5dd7070Spatrick   return nullptr;
69e5dd7070Spatrick }
70e5dd7070Spatrick 
71e5dd7070Spatrick } // end namespace tooling
72e5dd7070Spatrick } // end namespace clang
73