Skip to content

Commit

Permalink
[clang][dataflow] Bail out if input is Objective-C++. (#86479)
Browse files Browse the repository at this point in the history
We only ever intended to support C++, but the condition we were testing
allowed
Objective-C++ code by mistake.
  • Loading branch information
martinboehme committed Mar 25, 2024
1 parent 4bb9f91 commit e6f63a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion clang/lib/Analysis/FlowSensitive/AdornedCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ llvm::Expected<AdornedCFG> AdornedCFG::build(const Decl &D, Stmt &S,

// The shape of certain elements of the AST can vary depending on the
// language. We currently only support C++.
if (!C.getLangOpts().CPlusPlus)
if (!C.getLangOpts().CPlusPlus || C.getLangOpts().ObjC)
return llvm::createStringError(
std::make_error_code(std::errc::invalid_argument),
"Can only analyze C++");
Expand Down
4 changes: 3 additions & 1 deletion clang/unittests/Analysis/FlowSensitive/DeterminismTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ namespace clang::dataflow {
// flow-condition at function exit.
std::string analyzeAndPrintExitCondition(llvm::StringRef Code) {
DataflowAnalysisContext DACtx(std::make_unique<WatchedLiteralsSolver>());
clang::TestAST AST(Code);
TestInputs Inputs(Code);
Inputs.Language = TestLanguage::Lang_CXX17;
clang::TestAST AST(Inputs);
const auto *Target =
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
Environment InitEnv(DACtx, *Target);
Expand Down
33 changes: 27 additions & 6 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
#include "clang/Analysis/FlowSensitive/Value.h"
#include "clang/Basic/LangStandard.h"
#include "clang/Testing/TestAST.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Testing/Support/Error.h"
Expand Down Expand Up @@ -135,12 +136,32 @@ const Formula &getFormula(const ValueDecl &D, const Environment &Env) {
}

TEST(TransferTest, CNotSupported) {
std::string Code = R"(
void target() {}
)";
ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(
Code, [](const auto &, auto &) {}, {BuiltinOptions{}},
LangStandard::lang_c89),
TestInputs Inputs("void target() {}");
Inputs.Language = TestLanguage::Lang_C89;
clang::TestAST AST(Inputs);
const auto *Target =
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
ASSERT_THAT_ERROR(AdornedCFG::build(*Target).takeError(),
llvm::FailedWithMessage("Can only analyze C++"));
}

TEST(TransferTest, ObjectiveCNotSupported) {
TestInputs Inputs("void target() {}");
Inputs.Language = TestLanguage::Lang_OBJC;
clang::TestAST AST(Inputs);
const auto *Target =
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
ASSERT_THAT_ERROR(AdornedCFG::build(*Target).takeError(),
llvm::FailedWithMessage("Can only analyze C++"));
}

TEST(TransferTest, ObjectiveCXXNotSupported) {
TestInputs Inputs("void target() {}");
Inputs.Language = TestLanguage::Lang_OBJCXX;
clang::TestAST AST(Inputs);
const auto *Target =
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
ASSERT_THAT_ERROR(AdornedCFG::build(*Target).takeError(),
llvm::FailedWithMessage("Can only analyze C++"));
}

Expand Down

0 comments on commit e6f63a9

Please sign in to comment.