Skip to content

Commit e6f63a9

Browse files
authored
[clang][dataflow] Bail out if input is Objective-C++. (#86479)
We only ever intended to support C++, but the condition we were testing allowed Objective-C++ code by mistake.
1 parent 4bb9f91 commit e6f63a9

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

clang/lib/Analysis/FlowSensitive/AdornedCFG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ llvm::Expected<AdornedCFG> AdornedCFG::build(const Decl &D, Stmt &S,
144144

145145
// The shape of certain elements of the AST can vary depending on the
146146
// language. We currently only support C++.
147-
if (!C.getLangOpts().CPlusPlus)
147+
if (!C.getLangOpts().CPlusPlus || C.getLangOpts().ObjC)
148148
return llvm::createStringError(
149149
std::make_error_code(std::errc::invalid_argument),
150150
"Can only analyze C++");

clang/unittests/Analysis/FlowSensitive/DeterminismTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace clang::dataflow {
3030
// flow-condition at function exit.
3131
std::string analyzeAndPrintExitCondition(llvm::StringRef Code) {
3232
DataflowAnalysisContext DACtx(std::make_unique<WatchedLiteralsSolver>());
33-
clang::TestAST AST(Code);
33+
TestInputs Inputs(Code);
34+
Inputs.Language = TestLanguage::Lang_CXX17;
35+
clang::TestAST AST(Inputs);
3436
const auto *Target =
3537
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
3638
Environment InitEnv(DACtx, *Target);

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
1818
#include "clang/Analysis/FlowSensitive/Value.h"
1919
#include "clang/Basic/LangStandard.h"
20+
#include "clang/Testing/TestAST.h"
2021
#include "llvm/ADT/SmallVector.h"
2122
#include "llvm/ADT/StringRef.h"
2223
#include "llvm/Testing/Support/Error.h"
@@ -135,12 +136,32 @@ const Formula &getFormula(const ValueDecl &D, const Environment &Env) {
135136
}
136137

137138
TEST(TransferTest, CNotSupported) {
138-
std::string Code = R"(
139-
void target() {}
140-
)";
141-
ASSERT_THAT_ERROR(checkDataflowWithNoopAnalysis(
142-
Code, [](const auto &, auto &) {}, {BuiltinOptions{}},
143-
LangStandard::lang_c89),
139+
TestInputs Inputs("void target() {}");
140+
Inputs.Language = TestLanguage::Lang_C89;
141+
clang::TestAST AST(Inputs);
142+
const auto *Target =
143+
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
144+
ASSERT_THAT_ERROR(AdornedCFG::build(*Target).takeError(),
145+
llvm::FailedWithMessage("Can only analyze C++"));
146+
}
147+
148+
TEST(TransferTest, ObjectiveCNotSupported) {
149+
TestInputs Inputs("void target() {}");
150+
Inputs.Language = TestLanguage::Lang_OBJC;
151+
clang::TestAST AST(Inputs);
152+
const auto *Target =
153+
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
154+
ASSERT_THAT_ERROR(AdornedCFG::build(*Target).takeError(),
155+
llvm::FailedWithMessage("Can only analyze C++"));
156+
}
157+
158+
TEST(TransferTest, ObjectiveCXXNotSupported) {
159+
TestInputs Inputs("void target() {}");
160+
Inputs.Language = TestLanguage::Lang_OBJCXX;
161+
clang::TestAST AST(Inputs);
162+
const auto *Target =
163+
cast<FunctionDecl>(test::findValueDecl(AST.context(), "target"));
164+
ASSERT_THAT_ERROR(AdornedCFG::build(*Target).takeError(),
144165
llvm::FailedWithMessage("Can only analyze C++"));
145166
}
146167

0 commit comments

Comments
 (0)