[NFC] Optimize file kind determination (#139492)

There are checks in clang codebase that determine the type of source
file, associated with a given location - specifically, if it is an
ordonary file or comes from sources like command-line options or a
built-in definitions. These checks often rely on calls to
`getPresumedLoc`, which is relatively expensive. In certain cases, these
checks are combined, leading to repeated calculations of the costly
function negatively affecting compile time.

This change tries to optimize such checks. It must fix compile time
regression introduced in
https://github.com/llvm/llvm-project/pull/137306/.

---------

Co-authored-by: cor3ntin <corentinjabot@gmail.com>
This commit is contained in:
Serge Pavlov
2025-05-12 15:22:44 +07:00
committed by GitHub
parent 63f3a5babd
commit 5a1edf0f51
4 changed files with 14 additions and 9 deletions

View File

@@ -1529,6 +1529,15 @@ public:
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())

View File

@@ -305,8 +305,7 @@ public:
auto DefLoc = MI->getDefinitionLoc();
if (SM.isWrittenInBuiltinFile(DefLoc) ||
SM.isWrittenInCommandLineFile(DefLoc))
if (SM.isInPredefinedFile(DefLoc))
continue;
auto AssociatedModuleMacros = MD.getModuleMacros();

View File

@@ -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);

View File

@@ -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);
@@ -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