https://github.com/AnonMiraj created 
https://github.com/llvm/llvm-project/pull/237805

found this while looking for places we could optimize. 
`isLanguageDefinedBuiltin` checks the presumed file before the macro name. if 
we reorder the check it can result in speedups and I think it should not change 
the behaviour anywhere.

https://llvm-compile-time-tracker.com/compare.php?from=99a148b807da7942d629fd8e807d12026fcb5e20&to=55a467c5f3d2a5c5f6bd4a08102f0893367a5478&stat=instructions%3Au

>From 55a467c5f3d2a5c5f6bd4a08102f0893367a5478 Mon Sep 17 00:00:00 2001
From: Anonmiraj <[email protected]>
Date: Wed, 7 Oct 2026 16:16:14 +0300
Subject: [PATCH] [clang] Check the macro name before its presumed file

---
 clang/lib/Lex/PPDirectives.cpp | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index da0098f54bd334..43fb06372c0236 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -164,21 +164,13 @@ static bool isLanguageDefinedBuiltin(const SourceManager 
&SourceMgr,
   // defined.
   if (MI->isBuiltinMacro())
     return true;
-  // Builtin macros are defined in the builtin file
-  if (!SourceMgr.isWrittenInBuiltinFile(MI->getDefinitionLoc()))
+  // C uses __STDC* macros; C++ uses __cplusplus and __cpp* feature macros.
+  // Others aren't language-defined. Checking names is cheaper than finding 
files.
+  if (!MacroName.starts_with("__STDC") && MacroName != "__cplusplus" &&
+      !MacroName.starts_with("__cpp"))
     return false;
-  // C defines macros starting with __STDC, and C++ defines macros starting 
with
-  // __STDCPP
-  if (MacroName.starts_with("__STDC"))
-    return true;
-  // C++ defines the __cplusplus macro
-  if (MacroName == "__cplusplus")
-    return true;
-  // C++ defines various feature-test macros starting with __cpp
-  if (MacroName.starts_with("__cpp"))
-    return true;
-  // Anything else isn't language-defined
-  return false;
+  // Builtin macros are defined in the builtin file
+  return SourceMgr.isWrittenInBuiltinFile(MI->getDefinitionLoc());
 }
 
 static bool isReservedCXXAttributeName(Preprocessor &PP, IdentifierInfo *II) {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to