[clang] [clang][dataflow] Emit an error if source code is not compiled as C++. (PR #65301)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 14 11:21:52 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-analysis
Author: None (martinboehme)
<details>
<summary>Changes</summary>
The shape of certain elements of the AST can vary depending on the langugage.
We currently only support C++.
---
Full diff: https://github.com/llvm/llvm-project/pull/65301.diff
2 Files Affected:
- (modified) clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp (+7)
- (modified) clang/unittests/Analysis/FlowSensitive/TransferTest.cpp (+10)
``````````diff
diff --git a/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp b/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
index 7f9bc31bd41f2..004b7711a869d 100644
--- a/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
+++ b/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
@@ -84,6 +84,13 @@ ControlFlowContext::build(const Decl &D, Stmt &S, ASTContext &C) {
std::make_error_code(std::errc::invalid_argument),
"Cannot analyze templated declarations");
+ // The shape of certain elements of the AST can vary depending on the
+ // language. We currently only support C++.
+ if (!C.getLangOpts().CPlusPlus)
+ return llvm::createStringError(
+ std::make_error_code(std::errc::invalid_argument),
+ "Can only analyze C++");
+
CFG::BuildOptions Options;
Options.PruneTriviallyFalseEdges = true;
Options.AddImplicitDtors = true;
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 0a5cf62e5ea23..a312cd569a8ac 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -70,6 +70,16 @@ const Formula &getFormula(const ValueDecl &D, const Environment &Env) {
return cast<BoolValue>(Env.getValue(D))->formula();
}
+TEST(TransferTest, CNotSupported) {
+ std::string Code = R"(
+ void target() {}
+ )";
+ ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(
+ Code, [](const auto &, auto &) {}, {BuiltinOptions{}},
+ LangStandard::lang_c89),
+ llvm::FailedWithMessage("Can only analyze C++"));
+}
+
TEST(TransferTest, IntVarDeclNotTrackedWhenTransferDisabled) {
std::string Code = R"(
void target() {
``````````
</details>
https://github.com/llvm/llvm-project/pull/65301
More information about the cfe-commits
mailing list