Index: clang-tidy/modernize/UseUsingCheck.h =================================================================== --- clang-tidy/modernize/UseUsingCheck.h +++ clang-tidy/modernize/UseUsingCheck.h @@ -21,11 +21,15 @@ /// For the user-facing documentation see: /// http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-using.html class UseUsingCheck : public ClangTidyCheck { + public: - UseUsingCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + UseUsingCheck(StringRef Name, ClangTidyContext *Context); + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + +private: + const bool MacroWarning; }; } // namespace modernize Index: clang-tidy/modernize/UseUsingCheck.cpp =================================================================== --- clang-tidy/modernize/UseUsingCheck.cpp +++ clang-tidy/modernize/UseUsingCheck.cpp @@ -17,12 +17,20 @@ namespace tidy { namespace modernize { +UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + MacroWarning(Options.get("MacroWarning", false)) {} + void UseUsingCheck::registerMatchers(MatchFinder *Finder) { if (!getLangOpts().CPlusPlus11) return; Finder->addMatcher(typedefDecl().bind("typedef"), this); } +void UseUsingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "MacroWarning", MacroWarning); +} + // Checks if 'typedef' keyword can be removed - we do it only if // it is the only declaration in a declaration chain. static bool CheckRemoval(SourceManager &SM, SourceLocation StartLoc, @@ -80,22 +88,32 @@ auto &SM = *Result.SourceManager; if (auto *D = MatchedDecl->getUnderlyingType()->getAsCXXRecordDecl()) { - //TypeLoc TL = MatchedDecl->getTypeSourceInfo()->getTypeLoc(); + // TypeLoc TL = MatchedDecl->getTypeSourceInfo()->getTypeLoc(); llvm::errs() << D->getNameAsString() << "\n"; } + SourceLocation StartLoc = MatchedDecl->getLocStart(); + + if (StartLoc.isMacroID() && !MacroWarning) + return; + auto Diag = diag(MatchedDecl->getLocStart(), "use 'using' instead of 'typedef'"); - SourceLocation StartLoc = MatchedDecl->getLocStart(); - if (StartLoc.isMacroID()) + // Do not fix if there is macro or array. + if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID()) return; if (CheckRemoval(SM, StartLoc, Context)) { + auto printPolicy = PrintingPolicy(getLangOpts()); + printPolicy.SuppressScope = true; + printPolicy.ConstantArraySizeAsWritten = true; + printPolicy.UseVoidForZeroParams = false; + Diag << FixItHint::CreateReplacement( MatchedDecl->getSourceRange(), "using " + MatchedDecl->getNameAsString() + " = " + - MatchedDecl->getUnderlyingType().getAsString(getLangOpts())); + MatchedDecl->getUnderlyingType().getAsString(printPolicy)); } } Index: test/clang-tidy/modernize-use-using-macros.cpp =================================================================== --- /dev/null +++ test/clang-tidy/modernize-use-using-macros.cpp @@ -0,0 +1,23 @@ +// RUN: %check_clang_tidy %s modernize-use-using %t -- \ +// RUN: -config="{CheckOptions: [{key: modernize-use-using.MacroWarning, value: 1}]}" \ +// RUN: -- -std=c++11 -I %S/Inputs/modernize-use-using + +#define CODE typedef int INT + +CODE; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' +// CHECK-FIXES: #define CODE typedef int INT +// CHECK-FIXES: CODE; + +struct Foo; +#define Bar Baz +typedef Foo Bar; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' +// CHECK-FIXES: #define Bar Baz +// CHECK-FIXES: using Baz = Foo; + +#define TYPEDEF typedef +TYPEDEF Foo Bak; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' +// CHECK-FIXES: #define TYPEDEF typedef +// CHECK-FIXES: TYPEDEF Foo Bak; Index: test/clang-tidy/modernize-use-using.cpp =================================================================== --- test/clang-tidy/modernize-use-using.cpp +++ test/clang-tidy/modernize-use-using.cpp @@ -89,7 +89,6 @@ #define CODE typedef int INT CODE; -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' // CHECK-FIXES: #define CODE typedef int INT // CHECK-FIXES: CODE; @@ -102,7 +101,6 @@ #define TYPEDEF typedef TYPEDEF Foo Bak; -// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' // CHECK-FIXES: #define TYPEDEF typedef // CHECK-FIXES: TYPEDEF Foo Bak; @@ -148,3 +146,18 @@ struct { int d; } typedef S4; // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' // CHECK-FIXES: struct { int d; } typedef S4; + +namespace my_space { + class my_cclass {}; + typedef my_cclass FuncType; +// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' +// CHECK-FIXES: using FuncType = my_cclass +} + +#define lol 4 +typedef unsigned Map[lol]; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' + +typedef void (*fun_type)(); +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef' +// CHECK-FIXES: using fun_type = void (*)();