#include "UseAfterMoveCheck.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/ExprConcepts.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Lex/Lexer.h"
namespace {
+AST_MATCHER(Expr, hasUnevaluatedContext) {
+ if (isa<CXXNoexceptExpr>(Node) || isa<RequiresExpr>(Node))
+ return true;
+ if (const auto *UnaryExpr = dyn_cast<UnaryExprOrTypeTraitExpr>(&Node)) {
+ switch (UnaryExpr->getKind()) {
+ case UETT_SizeOf:
+ case UETT_AlignOf:
+ return true;
+ default:
+ return false;
+ }
+ }
+ if (const auto *TypeIDExpr = dyn_cast<CXXTypeidExpr>(&Node))
+ return !TypeIDExpr->isPotentiallyEvaluated();
+ return false;
+}
+
/// Contains information about a use-after-move.
struct UseAfterMove {
// The DeclRefExpr that constituted the use of the object.
static StatementMatcher inDecltypeOrTemplateArg() {
return anyOf(hasAncestor(typeLoc()),
hasAncestor(declRefExpr(
- to(functionDecl(ast_matchers::isTemplateInstantiation())))));
+ to(functionDecl(ast_matchers::isTemplateInstantiation())))),
+ hasAncestor(expr(hasUnevaluatedContext())));
}
UseAfterMoveFinder::UseAfterMoveFinder(ASTContext *TheContext)
C c;
}
};
+} // namespace PR33020
+
+namespace UnevalContext {
+struct Foo {};
+void noExcept() {
+ Foo Bar;
+ (void) noexcept(Foo{std::move(Bar)});
+ Foo Other{std::move(Bar)};
}
+void sizeOf() {
+ Foo Bar;
+ (void)sizeof(Foo{std::move(Bar)});
+ Foo Other{std::move(Bar)};
+}
+void alignOf() {
+ Foo Bar;
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-alignof-expression"
+ (void)alignof(Foo{std::move(Bar)});
+#pragma clang diagnostic pop
+ Foo Other{std::move(Bar)};
+}
+void typeId() {
+ Foo Bar;
+ // error: you need to include <typeinfo> before using the 'typeid' operator
+ // (void) typeid(Foo{std::move(Bar)}).name();
+ Foo Other{std::move(Bar)};
+}
+} // namespace UnevalContext