[clang] [clang][dataflow] Add support for `CXXRewrittenBinaryOperator`. (PR #81086)
https://github.com/martinboehme closed https://github.com/llvm/llvm-project/pull/81086 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][dataflow] Add support for `CXXRewrittenBinaryOperator`. (PR #81086)
https://github.com/Xazax-hun approved this pull request. https://github.com/llvm/llvm-project/pull/81086 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][dataflow] Add support for `CXXRewrittenBinaryOperator`. (PR #81086)
llvmbot wrote: @llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-analysis Author: None (martinboehme) Changes 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`. --- Full diff: https://github.com/llvm/llvm-project/pull/81086.diff 2 Files Affected: - (modified) clang/lib/Analysis/FlowSensitive/Transfer.cpp (+4) - (modified) clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp (+15-1) ``diff diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp index bb3aec763c29ca..a098471d0ee905 100644 --- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp +++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp @@ -545,6 +545,10 @@ class TransferVisitor : public ConstStmtVisitor { 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(); diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp index 73fb4063d92be9..b6e4973fd7cb2b 100644 --- a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp @@ -770,12 +770,17 @@ constexpr bool operator!=(const optional &lhs, const optional &rhs); template constexpr bool operator==(const optional &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 constexpr bool operator==(nullopt_t, const optional &opt); template constexpr bool operator!=(const optional &opt, nullopt_t); template constexpr bool operator!=(nullopt_t, const optional &opt); +#endif // __cplusplus < 202002L template constexpr bool operator==(const optional &opt, const U &value); @@ -1289,6 +1294,15 @@ class UncheckedOptionalAccessTest template 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 + void ExpectDiagnosticsFor(std::string SourceCode, FuncDeclMatcher FuncMatcher, +const char *CxxMode) { ReplaceAllOccurrences(SourceCode, "$ns", GetParam().NamespaceName); ReplaceAllOccurrences(SourceCode, "$optional", GetParam().TypeName); @@ -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]( `` https://github.com/llvm/llvm-project/pull/81086 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][dataflow] Add support for `CXXRewrittenBinaryOperator`. (PR #81086)
https://github.com/martinboehme created https://github.com/llvm/llvm-project/pull/81086 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`. >From 855349a61a16871a0bbbde6b00a0c929453bb1a9 Mon Sep 17 00:00:00 2001 From: Martin Braenne Date: Thu, 8 Feb 2024 04:20:52 + Subject: [PATCH] [clang][dataflow] Add support for `CXXRewrittenBinaryOperator`. 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`. --- clang/lib/Analysis/FlowSensitive/Transfer.cpp| 4 .../UncheckedOptionalAccessModelTest.cpp | 16 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp index bb3aec763c29c..a098471d0ee90 100644 --- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp +++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp @@ -545,6 +545,10 @@ class TransferVisitor : public ConstStmtVisitor { 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(); diff --git a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp index 73fb4063d92be..b6e4973fd7cb2 100644 --- a/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp @@ -770,12 +770,17 @@ constexpr bool operator!=(const optional &lhs, const optional &rhs); template constexpr bool operator==(const optional &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 constexpr bool operator==(nullopt_t, const optional &opt); template constexpr bool operator!=(const optional &opt, nullopt_t); template constexpr bool operator!=(nullopt_t, const optional &opt); +#endif // __cplusplus < 202002L template constexpr bool operator==(const optional &opt, const U &value); @@ -1289,6 +1294,15 @@ class UncheckedOptionalAccessTest template 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 + void ExpectDiagnosticsFor(std::string SourceCode, FuncDeclMatcher FuncMatcher, +const char *CxxMode) { ReplaceAllOccurrences(SourceCode, "$ns", GetParam().NamespaceName); ReplaceAllOccurrences(SourceCode, "$optional", GetParam().TypeName); @@ -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]( ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits