Skip to content

[NFC] Optimize file kind determination #139492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/include/clang/Basic/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
return Filename == "<scratch space>";
}

/// Returns whether \p Loc is located in a built-in or command line source.
bool isInPredefinedFile(SourceLocation Loc) const {
PresumedLoc Presumed = getPresumedLoc(Loc);
if (Presumed.isInvalid())
return false;
StringRef Filename(Presumed.getFilename());
return Filename == "<built-in>" || Filename == "<command line>";
}

/// Returns if a SourceLocation is in a system header.
bool isInSystemHeader(SourceLocation Loc) const {
if (Loc.isInvalid())
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ class MacroCallback : public PPCallbacks {

auto DefLoc = MI->getDefinitionLoc();

if (SM.isWrittenInBuiltinFile(DefLoc) ||
SM.isWrittenInCommandLineFile(DefLoc))
if (SM.isInPredefinedFile(DefLoc))
continue;

auto AssociatedModuleMacros = MD.getModuleMacros();
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Frontend/PrintPreprocessedOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,7 @@ void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
SourceLocation DefLoc = MI->getDefinitionLoc();
if (DirectivesOnly && !MI->isUsed()) {
SourceManager &SM = PP.getSourceManager();
if (SM.isWrittenInBuiltinFile(DefLoc) ||
SM.isWrittenInCommandLineFile(DefLoc))
if (SM.isInPredefinedFile(DefLoc))
return;
}
MoveToLine(DefLoc, /*RequireStartOfLine=*/true);
Expand Down
8 changes: 3 additions & 5 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,8 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
// Macro names with reserved identifiers are accepted if built-in or passed
// through the command line (the later may be present if -dD was used to
// generate the preprocessed file).
bool IsBuiltinOrCmd = SourceMgr.isWrittenInBuiltinFile(MacroNameLoc) ||
SourceMgr.isWrittenInCommandLineFile(MacroNameLoc);
if (!IsBuiltinOrCmd && !SourceMgr.isInSystemHeader(MacroNameLoc)) {
if (!SourceMgr.isInPredefinedFile(MacroNameLoc) &&
!SourceMgr.isInSystemHeader(MacroNameLoc)) {
MacroDiag D = MD_NoWarn;
if (isDefineUndef == MU_Define) {
D = shouldWarnOnMacroDef(*this, II);
Expand Down Expand Up @@ -1706,8 +1705,7 @@ void Preprocessor::HandleDigitDirective(Token &DigitTok) {
// If a filename was present, read any flags that are present.
if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
return;
if (!SourceMgr.isWrittenInBuiltinFile(DigitTok.getLocation()) &&
!SourceMgr.isWrittenInCommandLineFile(DigitTok.getLocation()))
if (!SourceMgr.isInPredefinedFile(DigitTok.getLocation()))
Diag(StrTok, diag::ext_pp_gnu_line_directive);

// Exiting to an empty string means pop to the including file, so leave
Expand Down
Loading