This is an archive of the discontinued LLVM Phabricator instance.

[clang] Make sure argument expansion locations are correct in presence of predefined buffer
ClosedPublic

Authored by kadircet on Apr 22 2020, 8:50 AM.

Details

Summary

Macro argument expansion logic relies on skipping file IDs that created
as a result of an include. Unfortunately it fails to do that for
predefined buffer since it doesn't have a valid insertion location.

As a result of that any file ID created for an include inside the
predefined buffers breaks the traversal logic in
SourceManager::computeMacroArgsCache.

To fix this issue we first record number of created FIDs for predefined
buffer, and then skip them explicitly in source manager.

Another solution would be to just give predefined buffers a valid source
location, but it is unclear where that should be..

Diff Detail

Event Timeline

kadircet created this revision.Apr 22 2020, 8:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 22 2020, 8:50 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sammccall added inline comments.Apr 22 2020, 9:24 AM
clang/lib/Basic/SourceManager.cpp
1805

nit: reverse the order of this check to avoid the string compare?
You could also consider hoisting bool IsMainFile out of the loop, maybe the optimizer can see this but I think it's probably a readablity win anyway.

1806

I don't love having this code duplicated.

What about:

SourceLocation IncludeLoc = ...
bool IncludedFromMain = isInFileID(IncludeLoc, FID) || (IsMainFile && Filename = "<built-in>");
if (IncludedFromMain) {
  // increment ID
} else if (IncludeLoc.isValid()) { // included, but not from this file
  return
}
continue;
clang/lib/Lex/PPLexerChange.cpp
420

Can we ever get spurious equality here because both sides are 0?

kadircet marked 4 inline comments as done.Apr 22 2020, 10:30 AM
kadircet added inline comments.
clang/lib/Lex/PPLexerChange.cpp
420

the latter is set during Preprocessor::EnterMainSourceFile() which initializes the PP and all users seem to be calling it first, therefore I assumed it would alwyas be a valid file id.

putting a && getPredefinesFileID().isValid() to be on the safe side.

kadircet updated this revision to Diff 259332.Apr 22 2020, 10:31 AM
kadircet marked an inline comment as done.
  • address comments
sammccall accepted this revision.Apr 22 2020, 11:10 AM
sammccall added inline comments.
clang/lib/Basic/SourceManager.cpp
1816

wasn't included from FID -> was included but not from FID

This revision is now accepted and ready to land.Apr 22 2020, 11:10 AM
This revision was automatically updated to reflect the committed changes.
kadircet marked an inline comment as done.