Skip to content

Commit c0703ea

Browse files
authored
[clang][dataflow] Emit an error if source code is not compiled as C++. (#65301)
The shape of certain elements of the AST can vary depending on the langugage. We currently only support C++.
1 parent be12f26 commit c0703ea

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ ControlFlowContext::build(const Decl &D, Stmt &S, ASTContext &C) {
8484
std::make_error_code(std::errc::invalid_argument),
8585
"Cannot analyze templated declarations");
8686

87+
// The shape of certain elements of the AST can vary depending on the
88+
// language. We currently only support C++.
89+
if (!C.getLangOpts().CPlusPlus)
90+
return llvm::createStringError(
91+
std::make_error_code(std::errc::invalid_argument),
92+
"Can only analyze C++");
93+
8794
CFG::BuildOptions Options;
8895
Options.PruneTriviallyFalseEdges = true;
8996
Options.AddImplicitDtors = true;

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ const Formula &getFormula(const ValueDecl &D, const Environment &Env) {
7070
return cast<BoolValue>(Env.getValue(D))->formula();
7171
}
7272

73+
TEST(TransferTest, CNotSupported) {
74+
std::string Code = R"(
75+
void target() {}
76+
)";
77+
ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(
78+
Code, [](const auto &, auto &) {}, {BuiltinOptions{}},
79+
LangStandard::lang_c89),
80+
llvm::FailedWithMessage("Can only analyze C++"));
81+
}
82+
7383
TEST(TransferTest, IntVarDeclNotTrackedWhenTransferDisabled) {
7484
std::string Code = R"(
7585
void target() {

0 commit comments

Comments
 (0)