Skip to content

Commit

Permalink
Cache FileID when translating diagnostics in PCH files
Browse files Browse the repository at this point in the history
Modules/preambles/PCH files can contain diagnostics, which, when used,
are added to the current ASTUnit. For that to work, they are translated
to use the current FileManager's FileIDs. When the entry is not the
main file, all local source locations will be checked by a linear
search. Now this is a problem, when there are lots of diagnostics (say,
25000) and lots of local source locations (say, 440000), and end up
taking seconds when using such a preamble.

The fix is to cache the last FileID, because many subsequent diagnostics
refer to the same file. This reduces the time spent in
ASTUnit::TranslateStoredDiagnostics from seconds to a few milliseconds
for files with many slocs/diagnostics.

This fixes PR31353.
Differential Revision: https://reviews.llvm.org/D29755

llvm-svn: 295301
  • Loading branch information
erikjv committed Feb 16, 2017
1 parent 21c3d8e commit 2c7c38d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion clang/lib/Frontend/ASTUnit.cpp
Expand Up @@ -2541,14 +2541,19 @@ void ASTUnit::TranslateStoredDiagnostics(

SmallVector<StoredDiagnostic, 4> Result;
Result.reserve(Diags.size());
const FileEntry *PreviousFE = nullptr;
FileID FID;
for (const StandaloneDiagnostic &SD : Diags) {
// Rebuild the StoredDiagnostic.
if (SD.Filename.empty())
continue;
const FileEntry *FE = FileMgr.getFile(SD.Filename);
if (!FE)
continue;
FileID FID = SrcMgr.translateFile(FE);
if (FE != PreviousFE) {
FID = SrcMgr.translateFile(FE);
PreviousFE = FE;
}
SourceLocation FileLoc = SrcMgr.getLocForStartOfFile(FID);
if (FileLoc.isInvalid())
continue;
Expand Down

0 comments on commit 2c7c38d

Please sign in to comment.