Skip to content

Commit f26f6de

Browse files
committed
Fix translateStandaloneDiag
1 parent 305e471 commit f26f6de

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

clang/include/clang/Frontend/StandaloneDiagnostic.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ struct StandaloneDiagnostic {
7373
///
7474
/// This allows the diagnostic to be emitted using the diagnostics engine, since
7575
/// StandaloneDiagnostics themselfs cannot be emitted directly.
76-
StoredDiagnostic translateStandaloneDiag(FileManager &FileMgr,
77-
SourceManager &SrcMgr,
78-
StandaloneDiagnostic &&StandaloneDiag);
76+
StoredDiagnostic
77+
translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
78+
const StandaloneDiagnostic &StandaloneDiag,
79+
llvm::StringMap<SourceLocation> &SrcLocCache);
7980

8081
} // namespace clang
8182

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,13 +1223,15 @@ bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps,
12231223
return true;
12241224

12251225
if (SavedMainFileBuffer) {
1226+
StoredDiagnostics.clear();
12261227
StoredDiagnostics.reserve(PreambleDiagnostics.size());
1227-
llvm::transform(
1228-
std::move(PreambleDiagnostics), std::back_inserter(StoredDiagnostics),
1229-
[&](auto &&StandaloneDiag) {
1230-
return translateStandaloneDiag(getFileManager(), getSourceManager(),
1231-
std::move(StandaloneDiag));
1232-
});
1228+
llvm::transform(std::move(PreambleDiagnostics),
1229+
std::back_inserter(StoredDiagnostics),
1230+
[&](auto &&StandaloneDiag) {
1231+
return translateStandaloneDiag(
1232+
getFileManager(), getSourceManager(),
1233+
std::move(StandaloneDiag), PreambleSrcLocCache);
1234+
});
12331235
} else
12341236
PreambleSrcLocCache.clear();
12351237

clang/lib/Frontend/StandaloneDiagnostic.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,34 @@ StandaloneDiagnostic::StandaloneDiagnostic(const LangOptions &LangOpts,
5555

5656
StoredDiagnostic
5757
translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
58-
StandaloneDiagnostic &&StandaloneDiag) {
58+
const StandaloneDiagnostic &StandaloneDiag,
59+
llvm::StringMap<SourceLocation> &SrcLocCache) {
5960
const auto FileRef = FileMgr.getOptionalFileRef(StandaloneDiag.Filename);
6061
if (!FileRef)
6162
return StoredDiagnostic(StandaloneDiag.Level, StandaloneDiag.ID,
62-
std::move(StandaloneDiag.Message));
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 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+
// Store in cache
83+
SrcLocCache[StandaloneDiag.Filename] = FileLoc;
84+
}
6385

64-
const auto FileID =
65-
SrcMgr.getOrCreateFileID(*FileRef, StandaloneDiag.FileKind);
66-
const auto FileLoc = SrcMgr.getLocForStartOfFile(FileID);
67-
assert(FileLoc.isValid() && "StandaloneDiagnostic should only use FilePath "
68-
"for encoding a valid source location.");
6986
const auto DiagLoc = FileLoc.getLocWithOffset(StandaloneDiag.FileOffset);
7087
const FullSourceLoc Loc(DiagLoc, SrcMgr);
7188

@@ -77,16 +94,16 @@ translateStandaloneDiag(FileManager &FileMgr, SourceManager &SrcMgr,
7794
/*IsTokenRange*/ false);
7895
};
7996

80-
SmallVector<CharSourceRange, 0> TranslatedRanges;
97+
SmallVector<CharSourceRange, 4> TranslatedRanges;
8198
TranslatedRanges.reserve(StandaloneDiag.Ranges.size());
8299
transform(StandaloneDiag.Ranges, std::back_inserter(TranslatedRanges),
83100
ConvertOffsetRange);
84101

85-
SmallVector<FixItHint, 0> TranslatedFixIts;
102+
SmallVector<FixItHint, 2> TranslatedFixIts;
86103
TranslatedFixIts.reserve(StandaloneDiag.FixIts.size());
87104
for (const auto &FixIt : StandaloneDiag.FixIts) {
88105
FixItHint TranslatedFixIt;
89-
TranslatedFixIt.CodeToInsert = std::string(FixIt.CodeToInsert);
106+
TranslatedFixIt.CodeToInsert = FixIt.CodeToInsert;
90107
TranslatedFixIt.RemoveRange = ConvertOffsetRange(FixIt.RemoveRange);
91108
TranslatedFixIt.InsertFromRange = ConvertOffsetRange(FixIt.InsertFromRange);
92109
TranslatedFixIt.BeforePreviousInsertions = FixIt.BeforePreviousInsertions;

0 commit comments

Comments
 (0)