Skip to content

Commit 4f5175f

Browse files
authored
[clang][cache] Work around crash in diagnostics syntax highlighting (swiftlang#9173)
Since llvm#66514 Clang relies on the FileIDs and buffer pointers stored within Preprocessor to perform syntax highlighting in diagnostics. With compilation caching, we round-trip diagnostics through serialization that uses a separate copy of SourceManager. This assigns different FileIDs and allocates buffers anew, breaking invariants of the consumer. This fix is not easy to test, since we'd rely on the order FileIDs get created, where in memory the buffers get allocated and the checkpoint interval. rdar://134198448 (cherry picked from commit 3613d18)
1 parent de75787 commit 4f5175f

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

clang/lib/Frontend/CachedDiagnostics.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,8 @@ struct CachingDiagnosticsProcessor::DiagnosticsConsumer
669669
// de-canonicalized filenames during compilation (the original diagnostic
670670
// uses canonical paths).
671671
assert(Serializer.DiagEngine.getClient() == OrigConsumer);
672+
// FIXME: This is not sound: giving the original consumer diagnostics with
673+
// different SourceManager may break them.
672674
Serializer.DiagEngine.Report(NewDiag);
673675
} else {
674676
OrigConsumer->HandleDiagnostic(Level, Info);

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ highlightLines(StringRef FileData, unsigned StartLineNumber,
11381138
std::make_unique<SmallVector<TextDiagnostic::StyleRange>[]>(
11391139
EndLineNumber - StartLineNumber + 1);
11401140

1141-
if (!PP || !ShowColors)
1141+
if (!PP || &PP->getSourceManager() != &SM || !ShowColors)
11421142
return SnippetRanges;
11431143

11441144
// Might cause emission of another diagnostic.

0 commit comments

Comments
 (0)