[clang-tools-extra] r339433 - [clang-tidy: modernize] modernize-redundant-void-arg crashes when a function body is in a macro

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 10 06:59:33 PDT 2018


Author: alexfh
Date: Fri Aug 10 06:59:33 2018
New Revision: 339433

URL: http://llvm.org/viewvc/llvm-project?rev=339433&view=rev
Log:
[clang-tidy: modernize] modernize-redundant-void-arg crashes when a function body is in a macro

Fixes https://bugs.llvm.org/show_bug.cgi?id=28406

Patch by IdrissRio.

Differential revision: https://reviews.llvm.org/D49800

Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/RedundantVoidArgCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/modernize-redundant-void-arg.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/RedundantVoidArgCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/RedundantVoidArgCheck.cpp?rev=339433&r1=339432&r2=339433&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/RedundantVoidArgCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/RedundantVoidArgCheck.cpp Fri Aug 10 06:59:33 2018
@@ -149,6 +149,8 @@ void RedundantVoidArgCheck::removeVoidAr
           ProtoToken.getRawIdentifier() == "void") {
         State = SawVoid;
         VoidToken = ProtoToken;
+      } else if (ProtoToken.is(tok::TokenKind::l_paren)) {
+        State = SawLeftParen;
       } else {
         State = NothingYet;
       }
@@ -235,10 +237,11 @@ void RedundantVoidArgCheck::processLambd
     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);
-    removeVoidArgumentTokens(Result, SourceRange(Begin, End),
+    SourceManager *SM = Result.SourceManager;
+    TypeLoc TL = Lambda->getLambdaClass()->getLambdaTypeInfo()->getTypeLoc();
+    removeVoidArgumentTokens(Result,
+                             {SM->getSpellingLoc(TL.getBeginLoc()),
+                              SM->getSpellingLoc(TL.getEndLoc())},
                              "lambda expression");
   }
 }

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-redundant-void-arg.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-redundant-void-arg.cpp?rev=339433&r1=339432&r2=339433&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-redundant-void-arg.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-redundant-void-arg.cpp Fri Aug 10 06:59:33 2018
@@ -445,3 +445,46 @@ struct DefinitionWithNoBody {
   // 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]]:48: 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]]:17: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+  // CHECK-FIXES: (void)WRAP([]() {});
+
+  [](void) BODY;
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant void argument list in lambda expression [modernize-redundant-void-arg]
+  // CHECK-FIXES: []() BODY;
+}




More information about the cfe-commits mailing list