llvm-project/clang/lib/Serialization
Nico Weber 728894340f 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
2014-09-06 01:25:55 +00:00
..
ASTCommon.cpp [modules] Make NamespaceAliasDecl redeclarable, as it should be. This fixes 2014-09-03 23:11:22 +00:00
ASTCommon.h [modules] Number anonymous declarations that are lexically within mergeable 2014-08-28 01:33:39 +00:00
ASTReader.cpp Add -Wunused-local-typedef, a warning that finds unused local typedefs. 2014-09-06 01:25:55 +00:00
ASTReaderDecl.cpp [modules] Make NamespaceAliasDecl redeclarable, as it should be. This fixes 2014-09-03 23:11:22 +00:00
ASTReaderInternals.h Header guard canonicalization, clang part. 2014-08-13 16:25:19 +00:00
ASTReaderStmt.cpp [OPENMP] Extract common superclass from all the loop directives. No functional changes (having common superclass is convenient for future loop directives CodeGen implementation) 2014-08-19 11:27:13 +00:00
ASTWriter.cpp Add -Wunused-local-typedef, a warning that finds unused local typedefs. 2014-09-06 01:25:55 +00:00
ASTWriterDecl.cpp [modules] Make NamespaceAliasDecl redeclarable, as it should be. This fixes 2014-09-03 23:11:22 +00:00
ASTWriterStmt.cpp [OPENMP] Extract common superclass from all the loop directives. No functional changes (having common superclass is convenient for future loop directives CodeGen implementation) 2014-08-19 11:27:13 +00:00
CMakeLists.txt [CMake] Use LINK_LIBS instead of target_link_libraries(). 2014-02-26 06:41:29 +00:00
GeneratePCH.cpp [C++11] Use 'nullptr'. Serialization edition. 2014-05-22 05:54:18 +00:00
GlobalModuleIndex.cpp Return a std::unique_ptr from getBufferForFile. NFC. 2014-08-26 19:54:40 +00:00
Makefile
Module.cpp [C++11] Use 'nullptr'. Serialization edition. 2014-05-22 05:54:18 +00:00
ModuleManager.cpp Return a std::unique_ptr from getBufferForFile. NFC. 2014-08-26 19:54:40 +00:00