[PATCH] D49800: [clang-tidy: modernize] modernize-redundant-void-arg crashes when a function body is in a macro

Idriss via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 9 17:45:17 PDT 2018


IdrissRio updated this revision to Diff 160039.
IdrissRio added a comment.

Yes it works also for the non macro. It is actually more general.
This update have passed all the tests.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49800

Files:
  clang-tidy/modernize/RedundantVoidArgCheck.cpp
  test/clang-tidy/modernize-redundant-void-arg.cpp


Index: test/clang-tidy/modernize-redundant-void-arg.cpp
===================================================================
--- test/clang-tidy/modernize-redundant-void-arg.cpp
+++ test/clang-tidy/modernize-redundant-void-arg.cpp
@@ -445,3 +445,49 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: {{.*}} in function definition
   // CHECK-FIXES: DefinitionWithNoBody() = delete;
 };
+
+
+
+#define BODY {}
+#define LAMBDA1 [](void){}
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+// CHECK-FIXES: LAMBDA1 [](){}
+
+#define LAMBDA2 [](void)BODY
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+// CHECK-FIXES: LAMBDA2 []()BODY
+
+#define LAMBDA3(captures, args, body) captures args body
+#define WRAP(...) __VA_ARGS__
+
+#define LAMBDA4 (void)LAMBDA3([],(void),BODY)
+// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+// CHECK-FIXES: LAMBDA4 (void)LAMBDA3([],(),BODY)
+
+#define LAMBDA5 []() -> void (*)(void) {return BODY;}
+// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+// CHECK-FIXES: LAMBDA5 []() -> void (*)() {return BODY;}
+void lambda_expression_with_macro_test(){
+  (void)LAMBDA1;
+  (void)LAMBDA2;
+  (void)LAMBDA3([], (void), BODY);
+  // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+  // CHECK-FIXES: (void)LAMBDA3([], (), BODY);
+
+  LAMBDA4;
+  LAMBDA5;
+	WRAP((void)WRAP(WRAP(LAMBDA3(WRAP([]), WRAP((void)), WRAP(BODY)))));
+  // CHECK-MESSAGES: :[[@LINE-1]]:47: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+  // CHECK-FIXES: WRAP((void)WRAP(WRAP(LAMBDA3(WRAP([]), WRAP(()), WRAP(BODY)))));
+
+	(void)WRAP([](void){});
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+  // CHECK-FIXES: (void)WRAP([](){});
+  
+	[](void)BODY;
+  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+  // CHECK-FIXES: []()BODY;
+}
+
+
+
Index: clang-tidy/modernize/RedundantVoidArgCheck.cpp
===================================================================
--- clang-tidy/modernize/RedundantVoidArgCheck.cpp
+++ clang-tidy/modernize/RedundantVoidArgCheck.cpp
@@ -149,6 +149,8 @@
           ProtoToken.getRawIdentifier() == "void") {
         State = SawVoid;
         VoidToken = ProtoToken;
+      } else if (ProtoToken.is(tok::TokenKind::l_paren)) {
+        State = SawLeftParen;
       } else {
         State = NothingYet;
       }
@@ -235,9 +237,11 @@
     const MatchFinder::MatchResult &Result, const LambdaExpr *Lambda) {
   if (Lambda->getLambdaClass()->getLambdaCallOperator()->getNumParams() == 0 &&
       Lambda->hasExplicitParameters()) {
-    SourceLocation Begin =
-        Lambda->getIntroducerRange().getEnd().getLocWithOffset(1);
-    SourceLocation End = Lambda->getBody()->getBeginLoc().getLocWithOffset(-1);
+    SourceLocation End, Begin;
+    auto SM = Result.SourceManager;
+    auto TypeLoc = Lambda->getLambdaClass()->getLambdaTypeInfo()->getTypeLoc();
+    End = SM->getSpellingLoc(TypeLoc.getLocEnd());
+    Begin = SM->getSpellingLoc(TypeLoc.getLocStart());
     removeVoidArgumentTokens(Result, SourceRange(Begin, End),
                              "lambda expression");
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49800.160039.patch
Type: text/x-patch
Size: 3607 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180810/19bb68e6/attachment-0001.bin>


More information about the cfe-commits mailing list