clang  9.0.0svn
Diagnostic.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/STLExtras.h"
16 
17 namespace clang {
18 namespace tooling {
19 
20 DiagnosticMessage::DiagnosticMessage(llvm::StringRef Message)
21  : Message(Message), FileOffset(0) {}
22 
24  const SourceManager &Sources,
25  SourceLocation Loc)
26  : Message(Message), FileOffset(0) {
27  assert(Loc.isValid() && Loc.isFileID());
28  FilePath = Sources.getFilename(Loc);
29 
30  // Don't store offset in the scratch space. It doesn't tell anything to the
31  // user. Moreover, it depends on the history of macro expansions and thus
32  // prevents deduplication of warnings in headers.
33  if (!FilePath.empty())
34  FileOffset = Sources.getFileOffset(Loc);
35 }
36 
37 Diagnostic::Diagnostic(llvm::StringRef DiagnosticName,
38  Diagnostic::Level DiagLevel, StringRef BuildDirectory)
39  : DiagnosticName(DiagnosticName), DiagLevel(DiagLevel),
40  BuildDirectory(BuildDirectory) {}
41 
45  Level DiagLevel, llvm::StringRef BuildDirectory)
46  : DiagnosticName(DiagnosticName), Message(Message), Notes(Notes),
47  DiagLevel(DiagLevel), BuildDirectory(BuildDirectory) {}
48 
49 const llvm::StringMap<Replacements> *selectFirstFix(const Diagnostic& D) {
50  if (!D.Message.Fix.empty())
51  return &D.Message.Fix;
52  auto Iter = llvm::find_if(D.Notes, [](const tooling::DiagnosticMessage &D) {
53  return !D.Fix.empty();
54  });
55  if (Iter != D.Notes.end())
56  return &Iter->Fix;
57  return nullptr;
58 }
59 
60 } // end namespace tooling
61 } // end namespace clang
DiagnosticMessage(llvm::StringRef Message="")
Definition: Diagnostic.cpp:20
Defines the SourceManager interface.
const llvm::StringMap< Replacements > * selectFirstFix(const Diagnostic &D)
Get the first fix to apply for this diagnostic.
Definition: Diagnostic.cpp:49
Level DiagLevel
Diagnostic level. Can indicate either an error or a warning.
Definition: Diagnostic.h:77
Represents the diagnostic with the level of severity and possible fixes to be applied.
Definition: Diagnostic.h:52
std::string BuildDirectory
A build directory of the diagnostic source file.
Definition: Diagnostic.h:86
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:68
SmallVector< DiagnosticMessage, 1 > Notes
Potential notes about the diagnostic.
Definition: Diagnostic.h:74
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
Represents the diagnostic message with the error message associated and the information on the locati...
Definition: Diagnostic.h:31
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
Encodes a location in the source.
DiagnosticMessage Message
Message associated to the diagnostic.
Definition: Diagnostic.h:71
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.