[clang] a117a65 - [analyzer][NFC] Out-line AnalysisManager::isInCodeFile (#246960)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 06:20:42 PST 2026
Author: Balázs Benics
Date: 2026-11-29T14:20:36Z
New Revision: a117a655ecc64473f3f7b95551f96bef9eb6c9f1
URL: https://github.com/llvm/llvm-project/commit/a117a655ecc64473f3f7b95551f96bef9eb6c9f1
DIFF: https://github.com/llvm/llvm-project/commit/a117a655ecc64473f3f7b95551f96bef9eb6c9f1.diff
LOG: [analyzer][NFC] Out-line AnalysisManager::isInCodeFile (#246960)
- Move the `AnalysisManager::isInCodeFile` body from `AnalysisManager.h`
into `AnalysisManager.cpp`.
- It instantiated `llvm::StringSwitch<bool>` and the `SourceManager`
queries in every TU including the header, i.e. every checker, though
only a handful of places call it.
- The header no longer needs `clang/Basic/SourceManager.h`; include it
explicitly in `CoreEngine.cpp`, `DeadStoresChecker.cpp` and
`PaddingChecker.cpp`, which relied on it transitively.
Build-time impact (instructions retired, 196 `lib/StaticAnalyzer` TUs):
- Total: -0.54% (run-to-run noise 0.2-0.4%).
- 111 TUs change by more than 0.15%, by -1.1% over those.
Assisted-by: Claude Code
Added:
Modified:
clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
index 2e3af6902d2f38..eb8399bab9c615 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
@@ -16,7 +16,6 @@
#include "clang/Analysis/AnalysisDeclContext.h"
#include "clang/Analysis/PathDiagnostic.h"
-#include "clang/Basic/SourceManager.h"
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
@@ -127,38 +126,7 @@ class AnalysisManager : public BugReporterData {
return AnaCtxMgr.getContext(D);
}
- bool isInCodeFile(SourceLocation SL) const {
- const SourceManager &SM = Ctx.getSourceManager();
- if (SM.isInMainFile(SL))
- return true;
-
- // Treat configured included source files #include "foo.cpp" same as the
- // main file.
- StringRef FileName = SM.getFilename(SL);
- for (StringRef Extension : UnityBuildFileExtensions)
- if (FileName.ends_with(Extension))
- return true;
-
- // Support the "unified sources" compilation method (eg. WebKit) that
- // involves producing non-header files that include other non-header files.
- // We should be included directly from a UnifiedSource* file
- // and we shouldn't be a header - which is a very safe defensive check.
- SourceLocation IL = SM.getIncludeLoc(SM.getFileID(SL));
- if (!IL.isValid() || !SM.isInMainFile(IL))
- return false;
- // Should rather be "file name starts with", but the current .getFilename
- // includes the full path.
- if (SM.getFilename(IL).contains("UnifiedSource")) {
- // It might be great to reuse FrontendOptions::getInputKindForExtension()
- // but for now it doesn't discriminate between code and header files.
- return llvm::StringSwitch<bool>(SM.getFilename(SL).rsplit('.').second)
- .Cases({"c", "m", "mm", "C", "cc", "cp"}, true)
- .Cases({"cpp", "CPP", "c++", "cxx", "cppm"}, true)
- .Default(false);
- }
-
- return false;
- }
+ bool isInCodeFile(SourceLocation SL) const;
};
} // enAnaCtxMgrspace
diff --git a/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
index c68ee72a9dbda5..9a4c554b44d2e6 100644
--- a/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
@@ -16,6 +16,7 @@
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/ParentMap.h"
#include "clang/Analysis/Analyses/LiveVariables.h"
+#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
diff --git a/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
index ac4b424dae6a21..9e8738ce1c2056 100644
--- a/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
@@ -15,6 +15,7 @@
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/RecordLayout.h"
+#include "clang/Basic/SourceManager.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
diff --git a/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp b/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
index f952f43e6f031c..ee70d11127ebf5 100644
--- a/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/AnalysisManager.cpp
@@ -8,6 +8,8 @@
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "clang/Basic/DiagnosticDriver.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/ADT/StringSwitch.h"
using namespace clang;
using namespace ento;
@@ -68,3 +70,36 @@ void AnalysisManager::FlushDiagnostics() {
Consumer->FlushDiagnostics(&filesMade);
}
}
+
+bool AnalysisManager::isInCodeFile(SourceLocation SL) const {
+ const SourceManager &SM = Ctx.getSourceManager();
+ if (SM.isInMainFile(SL))
+ return true;
+
+ // Treat configured included source files #include "foo.cpp" same as the
+ // main file.
+ StringRef FileName = SM.getFilename(SL);
+ for (StringRef Extension : UnityBuildFileExtensions)
+ if (FileName.ends_with(Extension))
+ return true;
+
+ // Support the "unified sources" compilation method (eg. WebKit) that
+ // involves producing non-header files that include other non-header files.
+ // We should be included directly from a UnifiedSource* file
+ // and we shouldn't be a header - which is a very safe defensive check.
+ SourceLocation IL = SM.getIncludeLoc(SM.getFileID(SL));
+ if (!IL.isValid() || !SM.isInMainFile(IL))
+ return false;
+ // Should rather be "file name starts with", but the current .getFilename
+ // includes the full path.
+ if (SM.getFilename(IL).contains("UnifiedSource")) {
+ // It might be great to reuse FrontendOptions::getInputKindForExtension()
+ // but for now it doesn't discriminate between code and header files.
+ return llvm::StringSwitch<bool>(SM.getFilename(SL).rsplit('.').second)
+ .Cases({"c", "m", "mm", "C", "cc", "cp"}, true)
+ .Cases({"cpp", "CPP", "c++", "cxx", "cppm"}, true)
+ .Default(false);
+ }
+
+ return false;
+}
diff --git a/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp b/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
index 9706e84eb131a1..32f3463a809011 100644
--- a/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
@@ -21,6 +21,7 @@
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/ProgramPoint.h"
#include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceManager.h"
#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/EntryPointStats.h"
More information about the cfe-commits
mailing list