summaryrefslogtreecommitdiffstats
path: root/clang/test
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2014-09-06 01:25:55 +0000
committerNico Weber <nicolasweber@gmx.de>2014-09-06 01:25:55 +0000
commit728894340f2a95eef5330b60410fb2abb158137b (patch)
tree88e99b74e1c5c9bd024dc17d9b63f31c1f5b5c83 /clang/test
parentaaa0b81a4e9273da01cb8350996f18c04fdd9871 (diff)
downloadbcm5719-llvm-728894340f2a95eef5330b60410fb2abb158137b.tar.gz
bcm5719-llvm-728894340f2a95eef5330b60410fb2abb158137b.zip
Add -Wunused-local-typedef, a warning that finds unused local typedefs.
The warning warns on TypedefNameDecls -- typedefs and C++11 using aliases -- that are !isReferenced(). Since the isReferenced() bit on TypedefNameDecls wasn't used for anything before this warning it wasn't always set correctly, so this patch also adds a few missing MarkAnyDeclReferenced() calls in various places for TypedefNameDecls. This is made a bit complicated due to local typedefs possibly being used only after their local scope has closed. Consider: template <class T> void template_fun(T t) { typename T::Foo s3foo; // YYY (void)s3foo; } void template_fun_user() { struct Local { typedef int Foo; // XXX } p; template_fun(p); } Here the typedef in XXX is only used at end-of-translation unit, when YYY in template_fun() gets instantiated. To handle this, typedefs that are unused when their scope exits are added to a set of potentially unused typedefs, and that set gets checked at end-of-TU. Typedefs that are still unused at that point then get warned on. There's also serialization code for this set, so that the warning works with precompiled headers and modules. For modules, the warning is emitted when the module is built, for precompiled headers each time the header gets used. Finally, consider a function using C++14 auto return types to return a local type defined in a header: auto f() { struct S { typedef int a; }; return S(); } Here, the typedef escapes its local scope and could be used by only some translation units including the header. To not warn on this, add a RecursiveASTVisitor that marks all delcs on local types returned from auto functions as referenced. (Except if it's a function with internal linkage, or the decls are private and the local type has no friends -- in these cases, it _is_ safe to warn.) Several of the included testcases (most of the interesting ones) were provided by Richard Smith. (gcc's spelling -Wunused-local-typedefs is supported as an alias for this warning.) llvm-svn: 217298
Diffstat (limited to 'clang/test')
-rw-r--r--clang/test/Misc/ast-dump-color.cpp2
-rw-r--r--clang/test/Modules/Inputs/module.map4
-rw-r--r--clang/test/Modules/Inputs/warn-unused-local-typedef.h1
-rw-r--r--clang/test/Modules/warn-unused-local-typedef.cpp9
-rw-r--r--clang/test/SemaCXX/implicit-exception-spec.cpp2
-rw-r--r--clang/test/SemaCXX/warn-unused-filescoped.cpp4
-rw-r--r--clang/test/SemaCXX/warn-unused-local-typedef-serialize.cpp11
-rw-r--r--clang/test/SemaCXX/warn-unused-local-typedef.cpp231
8 files changed, 260 insertions, 4 deletions
diff --git a/clang/test/Misc/ast-dump-color.cpp b/clang/test/Misc/ast-dump-color.cpp
index b4660b4a724..6e680a09a0f 100644
--- a/clang/test/Misc/ast-dump-color.cpp
+++ b/clang/test/Misc/ast-dump-color.cpp
@@ -89,7 +89,7 @@ struct Invalid {
//CHECK: {{^}}[[Blue]]| `-[[RESET]][[BLUE]]GuardedByAttr[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]col:29[[RESET]], [[Yellow]]col:43[[RESET]]>{{$}}
//CHECK: {{^}}[[Blue]]| `-[[RESET]][[MAGENTA]]DeclRefExpr[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]col:40[[RESET]]> [[Green]]'class Mutex':'class Mutex'[[RESET]][[Cyan]] lvalue[[RESET]][[Cyan]][[RESET]] [[GREEN]]Var[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]][[CYAN]] 'mu1'[[RESET]] [[Green]]'class Mutex':'class Mutex'[[RESET]]{{$}}
//CHECK: {{^}}[[Blue]]|-[[RESET]][[GREEN]]CXXRecordDecl[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]line:28:1[[RESET]], [[Yellow]]line:30:1[[RESET]]> [[Yellow]]line:28:8[[RESET]] struct[[CYAN]] Invalid[[RESET]] definition
-//CHECK: {{^}}[[Blue]]| |-[[RESET]][[GREEN]]CXXRecordDecl[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]col:1[[RESET]], [[Yellow]]col:8[[RESET]]> [[Yellow]]col:8[[RESET]] implicit struct[[CYAN]] Invalid[[RESET]]
+//CHECK: {{^}}[[Blue]]| |-[[RESET]][[GREEN]]CXXRecordDecl[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]col:1[[RESET]], [[Yellow]]col:8[[RESET]]> [[Yellow]]col:8[[RESET]] implicit referenced struct[[CYAN]] Invalid[[RESET]]
//CHECK: {{^}}[[Blue]]| |-[[RESET]][[GREEN]]CXXConstructorDecl[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]line:29:3[[RESET]], [[Yellow]]col:42[[RESET]]> [[Yellow]]col:29[[RESET]] invalid[[CYAN]] Invalid[[RESET]] [[Green]]'void (int)'[[RESET]]
//CHECK: {{^}}[[Blue]]| | |-[[RESET]][[GREEN]]ParmVarDecl[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]col:37[[RESET]], [[Yellow]]<invalid sloc>[[RESET]]> [[Yellow]]col:42[[RESET]] invalid [[Green]]'int'[[RESET]]
//CHECK: {{^}}[[Blue]]| | `-[[RESET]][[BLUE]]NoInlineAttr[[RESET]][[Yellow]] 0x{{[0-9a-fA-F]*}}[[RESET]] <[[Yellow]]col:18[[RESET]]>
diff --git a/clang/test/Modules/Inputs/module.map b/clang/test/Modules/Inputs/module.map
index e8011c70555..7040ee7e5ab 100644
--- a/clang/test/Modules/Inputs/module.map
+++ b/clang/test/Modules/Inputs/module.map
@@ -292,6 +292,10 @@ module warning {
header "warning.h"
}
+module warn_unused_local_typedef {
+ header "warn-unused-local-typedef.h"
+}
+
module initializer_list {
header "initializer_list"
}
diff --git a/clang/test/Modules/Inputs/warn-unused-local-typedef.h b/clang/test/Modules/Inputs/warn-unused-local-typedef.h
new file mode 100644
index 00000000000..6006de0cc8c
--- /dev/null
+++ b/clang/test/Modules/Inputs/warn-unused-local-typedef.h
@@ -0,0 +1 @@
+inline void myfun() { typedef int a; }
diff --git a/clang/test/Modules/warn-unused-local-typedef.cpp b/clang/test/Modules/warn-unused-local-typedef.cpp
new file mode 100644
index 00000000000..a463621d4ef
--- /dev/null
+++ b/clang/test/Modules/warn-unused-local-typedef.cpp
@@ -0,0 +1,9 @@
+// RUN: rm -rf %t
+// RUN: %clang -Wunused-local-typedef -c -x objective-c++ -fcxx-modules -fmodules -fmodules-cache-path=%t -I %S/Inputs %s 2>&1 | FileCheck %s -check-prefix=CHECK_1
+// RUN: %clang -Wunused-local-typedef -c -x objective-c++ -fcxx-modules -fmodules -fmodules-cache-path=%t -I %S/Inputs %s 2>&1 | FileCheck %s -check-prefix=CHECK_2 -allow-empty
+
+// For modules, the warning should only fire the first time, when the module is
+// built.
+// CHECK_1: warning: unused typedef
+// CHECK_2-NOT: warning: unused typedef
+@import warn_unused_local_typedef;
diff --git a/clang/test/SemaCXX/implicit-exception-spec.cpp b/clang/test/SemaCXX/implicit-exception-spec.cpp
index e26f985f0d0..6864f29dae3 100644
--- a/clang/test/SemaCXX/implicit-exception-spec.cpp
+++ b/clang/test/SemaCXX/implicit-exception-spec.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
+// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall -Wno-unused-local-typedefs %s
template<bool b> struct ExceptionIf { static int f(); };
template<> struct ExceptionIf<false> { typedef int f; };
diff --git a/clang/test/SemaCXX/warn-unused-filescoped.cpp b/clang/test/SemaCXX/warn-unused-filescoped.cpp
index df4c47e7178..18defee7d04 100644
--- a/clang/test/SemaCXX/warn-unused-filescoped.cpp
+++ b/clang/test/SemaCXX/warn-unused-filescoped.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -Wno-c++11-extensions -std=c++98 %s
-// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -Wno-unused-local-typedefs -Wno-c++11-extensions -std=c++98 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -Wno-unused-local-typedefs -std=c++11 %s
#ifdef HEADER
diff --git a/clang/test/SemaCXX/warn-unused-local-typedef-serialize.cpp b/clang/test/SemaCXX/warn-unused-local-typedef-serialize.cpp
new file mode 100644
index 00000000000..62630fc4ff7
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unused-local-typedef-serialize.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang -x c++-header -c -Wunused-local-typedef %s -o %t.gch -Werror
+// RUN: %clang -DBE_THE_SOURCE -c -Wunused-local-typedef -include %t %s 2>&1 | FileCheck %s
+// RUN: %clang -DBE_THE_SOURCE -c -Wunused-local-typedef -include %t %s 2>&1 | FileCheck %s
+
+#ifndef BE_THE_SOURCE
+inline void myfun() {
+// The warning should fire every time the pch file is used, not when it's built.
+// CHECK: warning: unused typedef
+ typedef int a;
+}
+#endif
diff --git a/clang/test/SemaCXX/warn-unused-local-typedef.cpp b/clang/test/SemaCXX/warn-unused-local-typedef.cpp
new file mode 100644
index 00000000000..0ff44b0082f
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unused-local-typedef.cpp
@@ -0,0 +1,231 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-local-typedef -verify -std=c++1y -fasm-blocks %s
+
+struct S {
+ typedef int Foo; // no diag
+};
+
+namespace N {
+ typedef int Foo; // no diag
+ typedef int Foo2; // no diag
+}
+
+template <class T> class Vec {};
+
+typedef int global_foo; // no diag
+
+void f() {
+ typedef int foo0; // expected-warning {{unused typedef 'foo0'}}
+ using foo0alias = int ; // expected-warning {{unused type alias 'foo0alias'}}
+
+ typedef int foo1 __attribute__((unused)); // no diag
+
+ typedef int foo2;
+ {
+ typedef int foo2; // expected-warning {{unused typedef 'foo2'}}
+ }
+ typedef foo2 foo3; // expected-warning {{unused typedef 'foo3'}}
+
+ typedef int foo2_2; // expected-warning {{unused typedef 'foo2_2'}}
+ {
+ typedef int foo2_2;
+ typedef foo2_2 foo3_2; // expected-warning {{unused typedef 'foo3_2'}}
+ }
+
+ typedef int foo4;
+ foo4 the_thing;
+
+ typedef int* foo5;
+ typedef foo5* foo6; // no diag
+ foo6 *myptr;
+
+ struct S2 {
+ typedef int Foo; // no diag
+ typedef int Foo2; // expected-warning {{unused typedef 'Foo2'}}
+
+ struct Deeper {
+ typedef int DeepFoo; // expected-warning {{unused typedef 'DeepFoo'}}
+ };
+ };
+
+ S2::Foo s2foo;
+
+ typedef struct {} foostruct; // expected-warning {{unused typedef 'foostruct'}}
+
+ typedef struct {} foostruct2; // no diag
+ foostruct2 fs2;
+
+ typedef int vecint; // no diag
+ Vec<vecint> v;
+
+ N::Foo nfoo;
+
+ typedef int ConstExprInt;
+ static constexpr int a = (ConstExprInt)4;
+}
+
+int printf(char const *, ...);
+
+void test() {
+ typedef signed long int superint; // no diag
+ printf("%f", (superint) 42);
+
+ typedef signed long int superint2; // no diag
+ printf("%f", static_cast<superint2>(42));
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-local-typedef"
+ typedef int trungl_bot_was_here; // no diag
+#pragma clang diagnostic pop
+
+ typedef int foo; // expected-warning {{unused typedef 'foo'}}
+}
+
+template <class T>
+void template_fun(T t) {
+ typedef int foo; // expected-warning {{unused typedef 'foo'}}
+ typedef int bar; // no-diag
+ bar asdf;
+
+ struct S2 {
+ typedef int Foo; // no diag
+
+ typedef int Foo2; // expected-warning {{unused typedef 'Foo2'}}
+
+ typedef int Foo3; // no diag
+ };
+
+ typename S2::Foo s2foo;
+ typename T::Foo s3foo;
+
+ typedef typename S2::Foo3 TTSF; // expected-warning {{unused typedef 'TTSF'}}
+}
+void template_fun_user() {
+ struct Local {
+ typedef int Foo; // no-diag
+ typedef int Bar; // expected-warning {{unused typedef 'Bar'}}
+ } p;
+ template_fun(p);
+}
+
+void typedef_in_nested_name() {
+ typedef struct {
+ typedef int Foo;
+ } A;
+ A::Foo adsf;
+
+ using A2 = struct {
+ typedef int Foo;
+ };
+ A2::Foo adsf2;
+}
+
+void use_in_asm() {
+ typedef struct {
+ int a;
+ int b;
+ } A;
+ __asm mov eax, [eax].A.b
+
+ using Alias = struct {
+ int a;
+ int b;
+ };
+ __asm mov eax, [eax].Alias.b
+}
+
+auto sneaky() {
+ struct S {
+ // Local typedefs can be used after the scope they were in has closed:
+ typedef int t;
+
+ // Even if they aren't, this could be an inline function that could be used
+ // in another TU, so this shouldn't warn either:
+ typedef int s;
+
+ private:
+ typedef int p; // expected-warning{{unused typedef 'p'}}
+ };
+ return S();
+}
+auto x = sneaky();
+decltype(x)::t y;
+
+static auto static_sneaky() {
+ struct S {
+ typedef int t;
+ // This function has internal linkage, so we can warn:
+ typedef int s; // expected-warning {{unused typedef 's'}}
+ };
+ return S();
+}
+auto sx = static_sneaky();
+decltype(sx)::t sy;
+
+auto sneaky_with_friends() {
+ struct S {
+ private:
+ friend class G;
+ // Can't warn if we have friends:
+ typedef int p;
+ };
+ return S();
+}
+
+namespace {
+auto nstatic_sneaky() {
+ struct S {
+ typedef int t;
+ // This function has internal linkage, so we can warn:
+ typedef int s; // expected-warning {{unused typedef 's'}}
+ };
+ return S();
+}
+auto nsx = nstatic_sneaky();
+decltype(nsx)::t nsy;
+}
+
+// Like sneaky(), but returning pointer to local type
+template<typename T>
+struct remove_reference { typedef T type; };
+template<typename T> struct remove_reference<T&> { typedef T type; };
+auto pointer_sneaky() {
+ struct S {
+ typedef int t;
+ typedef int s;
+ };
+ return (S*)nullptr;
+}
+remove_reference<decltype(*pointer_sneaky())>::type::t py;
+
+// Like sneaky(), but returning templated struct referencing local type.
+template <class T> struct container { int a; T t; };
+auto template_sneaky() {
+ struct S {
+ typedef int t;
+ typedef int s;
+ };
+ return container<S>();
+}
+auto tx = template_sneaky();
+decltype(tx.t)::t ty;
+
+// Like sneaky(), but doing its sneakiness by returning a member function
+// pointer.
+auto sneaky_memfun() {
+ struct S {
+ typedef int type;
+ int n;
+ };
+ return &S::n;
+}
+
+template <class T> void sneaky_memfun_g(int T::*p) {
+ typename T::type X;
+}
+
+void sneaky_memfun_h() {
+ sneaky_memfun_g(sneaky_memfun());
+}
+
+// This should not disable any warnings:
+#pragma clang diagnostic ignored "-Wunused-local-typedef"
OpenPOWER on IntegriCloud