Skip to content

Commit 5a1edf0

Browse files
spavloffcor3ntin
andauthored
[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 #137306. --------- Co-authored-by: cor3ntin <[email protected]>
1 parent 63f3a5b commit 5a1edf0

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,15 @@ class SourceManager : public RefCountedBase<SourceManager> {
15291529
return Filename == "<scratch space>";
15301530
}
15311531

1532+
/// Returns whether \p Loc is located in a built-in or command line source.
1533+
bool isInPredefinedFile(SourceLocation Loc) const {
1534+
PresumedLoc Presumed = getPresumedLoc(Loc);
1535+
if (Presumed.isInvalid())
1536+
return false;
1537+
StringRef Filename(Presumed.getFilename());
1538+
return Filename == "<built-in>" || Filename == "<command line>";
1539+
}
1540+
15321541
/// Returns if a SourceLocation is in a system header.
15331542
bool isInSystemHeader(SourceLocation Loc) const {
15341543
if (Loc.isInvalid())

clang/lib/ExtractAPI/ExtractAPIConsumer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ class MacroCallback : public PPCallbacks {
305305

306306
auto DefLoc = MI->getDefinitionLoc();
307307

308-
if (SM.isWrittenInBuiltinFile(DefLoc) ||
309-
SM.isWrittenInCommandLineFile(DefLoc))
308+
if (SM.isInPredefinedFile(DefLoc))
310309
continue;
311310

312311
auto AssociatedModuleMacros = MD.getModuleMacros();

clang/lib/Frontend/PrintPreprocessedOutput.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,7 @@ void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
569569
SourceLocation DefLoc = MI->getDefinitionLoc();
570570
if (DirectivesOnly && !MI->isUsed()) {
571571
SourceManager &SM = PP.getSourceManager();
572-
if (SM.isWrittenInBuiltinFile(DefLoc) ||
573-
SM.isWrittenInCommandLineFile(DefLoc))
572+
if (SM.isInPredefinedFile(DefLoc))
574573
return;
575574
}
576575
MoveToLine(DefLoc, /*RequireStartOfLine=*/true);

clang/lib/Lex/PPDirectives.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,8 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
374374
// Macro names with reserved identifiers are accepted if built-in or passed
375375
// through the command line (the later may be present if -dD was used to
376376
// generate the preprocessed file).
377-
bool IsBuiltinOrCmd = SourceMgr.isWrittenInBuiltinFile(MacroNameLoc) ||
378-
SourceMgr.isWrittenInCommandLineFile(MacroNameLoc);
379-
if (!IsBuiltinOrCmd && !SourceMgr.isInSystemHeader(MacroNameLoc)) {
377+
if (!SourceMgr.isInPredefinedFile(MacroNameLoc) &&
378+
!SourceMgr.isInSystemHeader(MacroNameLoc)) {
380379
MacroDiag D = MD_NoWarn;
381380
if (isDefineUndef == MU_Define) {
382381
D = shouldWarnOnMacroDef(*this, II);
@@ -1706,8 +1705,7 @@ void Preprocessor::HandleDigitDirective(Token &DigitTok) {
17061705
// If a filename was present, read any flags that are present.
17071706
if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
17081707
return;
1709-
if (!SourceMgr.isWrittenInBuiltinFile(DigitTok.getLocation()) &&
1710-
!SourceMgr.isWrittenInCommandLineFile(DigitTok.getLocation()))
1708+
if (!SourceMgr.isInPredefinedFile(DigitTok.getLocation()))
17111709
Diag(StrTok, diag::ext_pp_gnu_line_directive);
17121710

17131711
// Exiting to an empty string means pop to the including file, so leave

0 commit comments

Comments
 (0)