[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 Sep 4 23:49:38 PDT 2023


https://github.com/martinboehme created https://github.com/llvm/llvm-project/pull/65301:

The shape of certain elements of the AST can vary depending on the langugage.
We currently only support C++.


>From fc120d9d9df7cb02008f578219afb6a7d1977b83 Mon Sep 17 00:00:00 2001
From: Martin Braenne <mboehme at google.com>
Date: Tue, 5 Sep 2023 06:26:13 +0000
Subject: [PATCH] [clang][dataflow] Emit an error if source code is not
 compiled as C++.

The shape of certain elements of the AST can vary depending on the langugage.
We currently only support C++.
---
 .../lib/Analysis/FlowSensitive/ControlFlowContext.cpp  |  7 +++++++
 .../unittests/Analysis/FlowSensitive/TransferTest.cpp  | 10 ++++++++++
 2 files changed, 17 insertions(+)

diff --git a/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp b/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
index 7f9bc31bd41f2d..004b7711a869d1 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 0a5cf62e5ea233..a312cd569a8ac1 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() {



More information about the cfe-commits mailing list