From fc120d9d9df7cb02008f578219afb6a7d1977b83 Mon Sep 17 00:00:00 2001 From: Martin Braenne 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 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(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() {