[PATCH] D115576: [clang-tidy][#51939] Exempt placement-new expressions from 'bugprone-throw-keyword-missing'

Markus Böck via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 11 03:36:35 PST 2021


zero9178 created this revision.
zero9178 added reviewers: Szelethus, aaron.ballman, alexfh, hokein, whisperity.
Herald added subscribers: carlosgalvezp, rnkovacs, xazax.hun.
zero9178 requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

The purpose of this checker is to flag a missing throw keyword, and does so by checking for the construction of an exception class that is then unused. 
This works great except that placement new expressions are also flagged as those lead to the construction of an object as well, even though they are not temporary (as that is dependent on the storage). 
This patch fixes the issue by exempting the match if it is within a placement-new.

Fixes https://github.com/llvm/llvm-project/issues/51939


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115576

Files:
  clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone-throw-keyword-missing.cpp
@@ -175,3 +175,14 @@
 void exceptionRAIITest() {
   ExceptionRAII E;
 }
+
+namespace std {
+typedef decltype(sizeof(void*)) size_t;
+}
+
+void* operator new(std::size_t, void*);
+
+void placeMentNewTest() {
+  alignas(RegularException) unsigned char expr[sizeof(RegularException)];
+  new (expr) RegularException{};
+}
Index: clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/ThrowKeywordMissingCheck.cpp
@@ -24,11 +24,13 @@
       cxxConstructExpr(
           hasType(cxxRecordDecl(
               isSameOrDerivedFrom(matchesName("[Ee]xception|EXCEPTION")))),
-          unless(anyOf(hasAncestor(stmt(
-                           anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
-                       hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
-                       allOf(hasAncestor(CtorInitializerList),
-                             unless(hasAncestor(cxxCatchStmt()))))))
+          unless(anyOf(
+              hasAncestor(
+                  stmt(anyOf(cxxThrowExpr(), callExpr(), returnStmt()))),
+              hasAncestor(decl(anyOf(varDecl(), fieldDecl()))),
+              hasAncestor(expr(cxxNewExpr(hasAnyPlacementArg(anything())))),
+              allOf(hasAncestor(CtorInitializerList),
+                    unless(hasAncestor(cxxCatchStmt()))))))
           .bind("temporary-exception-not-thrown"),
       this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115576.393671.patch
Type: text/x-patch
Size: 1861 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211211/400a3806/attachment.bin>


More information about the cfe-commits mailing list