[PATCH] D61700: [clang-tidy] readability-redundant-declaration: fix false positive with C "extern inline"

Matthias Gehre via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 8 14:09:47 PDT 2019


mgehre created this revision.
mgehre added reviewers: alexfh, danielmarjamaki.
Herald added a subscriber: xazax.hun.
Herald added a project: clang.

readability-redundant-declaration was diagnosing a redundant declaration
on "extern inline void f();", which is needed in C code to force an external definition
of the inline function f. (This is different to how inline behaves in C++).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D61700

Files:
  clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
  clang-tools-extra/test/clang-tidy/readability-redundant-declaration.c
  clang-tools-extra/test/clang-tidy/readability-redundant-declaration.cpp


Index: clang-tools-extra/test/clang-tidy/readability-redundant-declaration.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/readability-redundant-declaration.cpp
+++ clang-tools-extra/test/clang-tidy/readability-redundant-declaration.cpp
@@ -68,3 +68,13 @@
 // CHECK-FIXES: {{^}}DEFINE(test);{{$}}
 
 } // namespace macros
+
+inline void g() {}
+
+inline void g(); // g
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant 'g' declaration
+// CHECK-FIXES: {{^}}// g{{$}}
+
+extern inline void g(); // extern g
+// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: redundant 'g' declaration
+// CHECK-FIXES: {{^}}// extern g{{$}}
\ No newline at end of file
Index: clang-tools-extra/test/clang-tidy/readability-redundant-declaration.c
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/readability-redundant-declaration.c
@@ -0,0 +1,31 @@
+// RUN: %check_clang_tidy %s readability-redundant-declaration %t
+
+extern int Xyz;
+extern int Xyz; // Xyz
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'Xyz' declaration [readability-redundant-declaration]
+// CHECK-FIXES: {{^}}// Xyz{{$}}
+int Xyz = 123;
+
+extern int A;
+extern int A, B;
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'A' declaration
+// CHECK-FIXES: {{^}}extern int A, B;{{$}}
+
+extern int Buf[10];
+extern int Buf[10]; // Buf[10]
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'Buf' declaration
+// CHECK-FIXES: {{^}}// Buf[10]{{$}}
+
+static int f();
+static int f(); // f
+// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: redundant 'f' declaration
+// CHECK-FIXES: {{^}}// f{{$}}
+static int f() {}
+
+inline void g() {}
+
+inline void g();
+// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant 'g' declaration
+
+// OK: Needed to emit an external definition.
+extern inline void g();
\ No newline at end of file
Index: clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
+++ clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
@@ -18,6 +18,10 @@
 namespace tidy {
 namespace readability {
 
+AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
+  return Node.doesDeclarationForceExternallyVisibleDefinition();
+}
+
 RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
                                                      ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
@@ -26,8 +30,10 @@
 void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       namedDecl(anyOf(varDecl(unless(isDefinition())),
-                      functionDecl(unless(anyOf(isDefinition(), isDefaulted(),
-                                                hasParent(friendDecl()))))))
+                      functionDecl(unless(anyOf(
+                          isDefinition(), isDefaulted(),
+                          doesDeclarationForceExternallyVisibleDefinition(),
+                          hasParent(friendDecl()))))))
           .bind("Decl"),
       this);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61700.198724.patch
Type: text/x-patch
Size: 3243 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190508/c933ecac/attachment-0001.bin>


More information about the cfe-commits mailing list