Skip to content

Commit

Permalink
[clang][dataflow] Add support for CXXRewrittenBinaryOperator. (#81086)
Browse files Browse the repository at this point in the history
This occurs in rewritten candidates for binary operators (a C++20
feature).

The patch modifies UncheckedOptionalAccessModelTest to run in C++20 mode
(as
well as C++17 mode, as before) and to use rewritten candidates. The
modified
test fails without the newly added support for
`CXXRewrittenBinaryOperator`.
  • Loading branch information
martinboehme committed Feb 8, 2024
1 parent 9ff3b82 commit a446c9b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
VisitCallExpr(S);
}

void VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *RBO) {
propagateValue(*RBO->getSemanticForm(), *RBO, Env);
}

void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
if (S->getCastKind() == CK_ConstructorConversion) {
const Expr *SubExpr = S->getSubExpr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,17 @@ constexpr bool operator!=(const optional<T> &lhs, const optional<U> &rhs);
template <typename T>
constexpr bool operator==(const optional<T> &opt, nullopt_t);
// C++20 and later do not define the following overloads because they are
// provided by rewritten candidates instead.
#if __cplusplus < 202002L
template <typename T>
constexpr bool operator==(nullopt_t, const optional<T> &opt);
template <typename T>
constexpr bool operator!=(const optional<T> &opt, nullopt_t);
template <typename T>
constexpr bool operator!=(nullopt_t, const optional<T> &opt);
#endif // __cplusplus < 202002L
template <typename T, typename U>
constexpr bool operator==(const optional<T> &opt, const U &value);
Expand Down Expand Up @@ -1289,6 +1294,15 @@ class UncheckedOptionalAccessTest
template <typename FuncDeclMatcher>
void ExpectDiagnosticsFor(std::string SourceCode,
FuncDeclMatcher FuncMatcher) {
// Run in C++17 and C++20 mode to cover differences in the AST between modes
// (e.g. C++20 can contain `CXXRewrittenBinaryOperator`).
for (const char *CxxMode : {"-std=c++17", "-std=c++20"})
ExpectDiagnosticsFor(SourceCode, FuncMatcher, CxxMode);
}

template <typename FuncDeclMatcher>
void ExpectDiagnosticsFor(std::string SourceCode, FuncDeclMatcher FuncMatcher,
const char *CxxMode) {
ReplaceAllOccurrences(SourceCode, "$ns", GetParam().NamespaceName);
ReplaceAllOccurrences(SourceCode, "$optional", GetParam().TypeName);

Expand Down Expand Up @@ -1332,7 +1346,7 @@ class UncheckedOptionalAccessTest
llvm::move(EltDiagnostics, std::back_inserter(Diagnostics));
})
.withASTBuildArgs(
{"-fsyntax-only", "-std=c++17", "-Wno-undefined-inline"})
{"-fsyntax-only", CxxMode, "-Wno-undefined-inline"})
.withASTBuildVirtualMappedFiles(
tooling::FileContentMappings(Headers.begin(), Headers.end())),
/*VerifyResults=*/[&Diagnostics](
Expand Down

0 comments on commit a446c9b

Please sign in to comment.