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.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217298 91177308-0d34-0410-b5e6-96231b3b80d8
Showing
- include/clang/Basic/DiagnosticGroups.td 4 additions, 1 deletioninclude/clang/Basic/DiagnosticGroups.td
- include/clang/Basic/DiagnosticSemaKinds.td 3 additions, 0 deletionsinclude/clang/Basic/DiagnosticSemaKinds.td
- include/clang/Sema/ExternalSemaSource.h 13 additions, 0 deletionsinclude/clang/Sema/ExternalSemaSource.h
- include/clang/Sema/MultiplexExternalSemaSource.h 9 additions, 0 deletionsinclude/clang/Sema/MultiplexExternalSemaSource.h
- include/clang/Sema/Sema.h 7 additions, 0 deletionsinclude/clang/Sema/Sema.h
- include/clang/Serialization/ASTBitCodes.h 4 additions, 1 deletioninclude/clang/Serialization/ASTBitCodes.h
- include/clang/Serialization/ASTReader.h 8 additions, 0 deletionsinclude/clang/Serialization/ASTReader.h
- lib/Sema/MultiplexExternalSemaSource.cpp 6 additions, 0 deletionslib/Sema/MultiplexExternalSemaSource.cpp
- lib/Sema/Sema.cpp 19 additions, 0 deletionslib/Sema/Sema.cpp
- lib/Sema/SemaCXXScopeSpec.cpp 3 additions, 0 deletionslib/Sema/SemaCXXScopeSpec.cpp
- lib/Sema/SemaDecl.cpp 39 additions, 3 deletionslib/Sema/SemaDecl.cpp
- lib/Sema/SemaStmt.cpp 40 additions, 0 deletionslib/Sema/SemaStmt.cpp
- lib/Sema/SemaStmtAsm.cpp 3 additions, 2 deletionslib/Sema/SemaStmtAsm.cpp
- lib/Sema/SemaTemplate.cpp 1 addition, 0 deletionslib/Sema/SemaTemplate.cpp
- lib/Sema/SemaTemplateInstantiateDecl.cpp 4 additions, 1 deletionlib/Sema/SemaTemplateInstantiateDecl.cpp
- lib/Serialization/ASTReader.cpp 18 additions, 0 deletionslib/Serialization/ASTReader.cpp
- lib/Serialization/ASTWriter.cpp 10 additions, 0 deletionslib/Serialization/ASTWriter.cpp
- test/Misc/ast-dump-color.cpp 1 addition, 1 deletiontest/Misc/ast-dump-color.cpp
- test/Modules/Inputs/module.map 4 additions, 0 deletionstest/Modules/Inputs/module.map
- test/Modules/Inputs/warn-unused-local-typedef.h 1 addition, 0 deletionstest/Modules/Inputs/warn-unused-local-typedef.h
Loading
Please register or sign in to comment