clang 22.0.0git
StandaloneDiagnostic.cpp
Go to the documentation of this file.
1//===--- StandaloneDiagnostic.h - Serializable Diagnostic ------------- ---===//
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
10#include "clang/Lex/Lexer.h"
11
12namespace clang {
13
16 const LangOptions &LangOpts) {
17 const auto FileRange = Lexer::makeFileCharRange(Range, SrcMgr, LangOpts);
18 Begin = SrcMgr.getFileOffset(FileRange.getBegin());
19 End = SrcMgr.getFileOffset(FileRange.getEnd());
20}
21
29
31 const StoredDiagnostic &InDiag)
32 : Level(InDiag.getLevel()), ID(InDiag.getID()),
33 Message(InDiag.getMessage()) {
34 const FullSourceLoc &FullLoc = InDiag.getLocation();
35 // This is not an invalid diagnostic; invalid SourceLocations are used to
36 // represent diagnostics without a specific SourceLocation.
37 if (FullLoc.isInvalid())
38 return;
39
40 const auto &SrcMgr = FullLoc.getManager();
41 FileKind = SrcMgr.getFileCharacteristic(static_cast<SourceLocation>(FullLoc));
42 const auto FileLoc = SrcMgr.getFileLoc(static_cast<SourceLocation>(FullLoc));
43 FileOffset = SrcMgr.getFileOffset(FileLoc);
44 Filename = SrcMgr.getFilename(FileLoc);
45 assert(!Filename.empty() && "diagnostic with location has no source file?");
46
47 Ranges.reserve(InDiag.getRanges().size());
48 for (const auto &Range : InDiag.getRanges())
49 Ranges.emplace_back(Range, SrcMgr, LangOpts);
50
51 FixIts.reserve(InDiag.getFixIts().size());
52 for (const auto &FixIt : InDiag.getFixIts())
53 FixIts.emplace_back(SrcMgr, LangOpts, FixIt);
54}
55
58 const StandaloneDiagnostic &StandaloneDiag,
59 llvm::StringMap<SourceLocation> &SrcLocCache) {
60 const auto FileRef = FileMgr.getOptionalFileRef(StandaloneDiag.Filename);
61 if (!FileRef)
62 return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
63 StandaloneDiag.Message);
64
65 // Try to get FileLoc from cache first
66 SourceLocation FileLoc;
67 auto It = SrcLocCache.find(StandaloneDiag.Filename);
68 if (It != SrcLocCache.end()) {
69 FileLoc = It->getValue();
70 }
71
72 // Cache miss - compute and cache the location
73 if (FileLoc.isInvalid()) {
74 const auto FileID =
75 SrcMgr.getOrCreateFileID(*FileRef, StandaloneDiag.FileKind);
76 FileLoc = SrcMgr.getLocForStartOfFile(FileID);
77
78 if (FileLoc.isInvalid())
79 return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
80 StandaloneDiag.Message);
81
82 SrcLocCache[StandaloneDiag.Filename] = FileLoc;
83 }
84
85 const auto DiagLoc = FileLoc.getLocWithOffset(StandaloneDiag.FileOffset);
86 const FullSourceLoc Loc(DiagLoc, SrcMgr);
87
88 auto ConvertOffsetRange =
90 return CharSourceRange(
91 SourceRange(FileLoc.getLocWithOffset(Range.Begin),
92 FileLoc.getLocWithOffset(Range.End)),
93 /*IsTokenRange*/ false);
94 };
95
96 SmallVector<CharSourceRange, 4> TranslatedRanges;
97 TranslatedRanges.reserve(StandaloneDiag.Ranges.size());
98 transform(StandaloneDiag.Ranges, std::back_inserter(TranslatedRanges),
99 ConvertOffsetRange);
100
101 SmallVector<FixItHint, 2> TranslatedFixIts;
102 TranslatedFixIts.reserve(StandaloneDiag.FixIts.size());
103 for (const auto &FixIt : StandaloneDiag.FixIts) {
104 FixItHint TranslatedFixIt;
105 TranslatedFixIt.CodeToInsert = FixIt.CodeToInsert;
106 TranslatedFixIt.RemoveRange = ConvertOffsetRange(FixIt.RemoveRange);
107 TranslatedFixIt.InsertFromRange = ConvertOffsetRange(FixIt.InsertFromRange);
108 TranslatedFixIt.BeforePreviousInsertions = FixIt.BeforePreviousInsertions;
109 TranslatedFixIts.push_back(std::move(TranslatedFixIt));
110 }
111
112 return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
113 StandaloneDiag.Message, Loc, TranslatedRanges,
114 TranslatedFixIts);
115}
116
117} // namespace clang
Represents a character-granular source range.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:53
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition Diagnostic.h:79
bool BeforePreviousInsertions
Definition Diagnostic.h:93
CharSourceRange RemoveRange
Code that should be replaced to correct the error.
Definition Diagnostic.h:83
CharSourceRange InsertFromRange
Code in the specific range that should be inserted in the insertion location.
Definition Diagnostic.h:87
std::string CodeToInsert
The actual code to insert at the insertion location, as a string.
Definition Diagnostic.h:91
A SourceLocation and its associated SourceManager.
const SourceManager & getManager() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
static CharSourceRange makeFileCharRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Accepts a range and returns a character range with file locations.
Definition Lexer.cpp:951
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Represents a diagnostic in a form that can be retained until its corresponding source manager is dest...
ArrayRef< FixItHint > getFixIts() const
ArrayRef< CharSourceRange > getRanges() const
const FullSourceLoc & getLocation() const
Public enums and private classes that are part of the SourceManager implementation.
The JSON file list parser is used to communicate input to InstallAPI.
for(const auto &A :T->param_types())
StoredDiagnostic translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr, const StandaloneDiagnostic &StandaloneDiag, llvm::StringMap< SourceLocation > &SrcLocCache)
Translates StandaloneDiag into a StoredDiagnostic, associating it with the provided FileManager and S...
Represents a CharSourceRange within a StandaloneDiagnostic.
SourceOffsetRange(CharSourceRange Range, const SourceManager &SrcMgr, const LangOptions &LangOpts)
StandaloneFixIt(const SourceManager &SrcMgr, const LangOptions &LangOpts, const FixItHint &FixIt)
Represents a StoredDiagnostic in a form that can be retained until after its SourceManager has been d...
SrcMgr::CharacteristicKind FileKind
std::vector< StandaloneFixIt > FixIts
StandaloneDiagnostic(const LangOptions &LangOpts, const StoredDiagnostic &InDiag)
std::vector< SourceOffsetRange > Ranges
DiagnosticsEngine::Level Level