njames93 updated this revision to Diff 240506.
njames93 added a comment.

- added more unevaluated context checks


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73441/new/

https://reviews.llvm.org/D73441

Files:
  clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
@@ -1271,3 +1271,28 @@
   }
 };
 }
+
+namespace PR44667 {
+#define REQUIRE(expr) (void)(expr);
+struct S {};
+
+void foo() {
+  S s;
+  REQUIRE(noexcept(S{std::move(s)}));
+  S other{std::move(s)};
+}
+void otherUnevaluated(){
+  S s;
+  REQUIRE(sizeof(S{std::move(s)}) > 8);
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-alignof-expression"
+  REQUIRE(alignof(S{std::move(s)}) > 8);
+#pragma clang diagnostic pop
+
+  // error: you need to include <typeinfo> before using the 'typeid' operator
+  // REQUIRE(typeid(S{std::move(s)}).name());
+
+  S other{std::move(s)};
+}
+} // namespace PR44667
Index: clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -8,6 +8,10 @@
 
 #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"
 
@@ -23,6 +27,22 @@
 
 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.
@@ -77,7 +97,8 @@
 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)


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-use-after-move.cpp
@@ -1271,3 +1271,28 @@
   }
 };
 }
+
+namespace PR44667 {
+#define REQUIRE(expr) (void)(expr);
+struct S {};
+
+void foo() {
+  S s;
+  REQUIRE(noexcept(S{std::move(s)}));
+  S other{std::move(s)};
+}
+void otherUnevaluated(){
+  S s;
+  REQUIRE(sizeof(S{std::move(s)}) > 8);
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-alignof-expression"
+  REQUIRE(alignof(S{std::move(s)}) > 8);
+#pragma clang diagnostic pop
+
+  // error: you need to include <typeinfo> before using the 'typeid' operator
+  // REQUIRE(typeid(S{std::move(s)}).name());
+
+  S other{std::move(s)};
+}
+} // namespace PR44667
Index: clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/UseAfterMoveCheck.cpp
@@ -8,6 +8,10 @@
 
 #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"
 
@@ -23,6 +27,22 @@
 
 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.
@@ -77,7 +97,8 @@
 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)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to