Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][dataflow] Emit an error if source code is not compiled as C++. #65301

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down