poelmanc created this revision. poelmanc added reviewers: alexfh, alexfh_. poelmanc added a project: clang-tools-extra. Herald added a project: clang. Herald added a subscriber: cfe-commits.
clang-tidy's modernize-use-using feature is great! But if it finds any commas that are not within parentheses, it won't create a fix. That means it won't change lines like: ` typedef std::pair<int, int> Point;` to ` using Point = std::pair<int, int>;` or even: ` typedef std::map<std::string, Foo> MyMap;` ` typedef std::vector<int,MyCustomAllocator<int>> MyVector;` This patch allows the fix to apply to lines with commas if they are within parentheses //or// angle brackets. One test is include Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D67460 Files: clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp clang-tools-extra/test/clang-tidy/modernize-use-using.cpp Index: clang-tools-extra/test/clang-tidy/modernize-use-using.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/modernize-use-using.cpp +++ clang-tools-extra/test/clang-tidy/modernize-use-using.cpp @@ -182,4 +182,11 @@ class E : public C<E> { void f() override { super::f(); } }; + +template <typename T1, typename T2> +class TwoArgTemplate { + typedef TwoArgTemplate<T1, T2> self; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' + // CHECK-FIXES: using self = TwoArgTemplate<T1, T2>; +}; } Index: clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp +++ clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp @@ -40,6 +40,7 @@ Token Tok; int ParenLevel = 0; + int AngleBracketLevel = 0; bool FoundTypedef = false; while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) { @@ -54,10 +55,16 @@ case tok::r_paren: ParenLevel--; break; + case tok::less: // '<', start template + AngleBracketLevel++; + break; + case tok::greater: // '>', end template + AngleBracketLevel--; + break; case tok::comma: - if (ParenLevel == 0) { - // If there is comma and we are not between open parenthesis then it is - // two or more declarations in this chain. + if (ParenLevel == 0 && AngleBracketLevel == 0) { + // If there is comma and we are not between open parenthesis or between + // open angle brackets then it is two or more declarations in this chain. return false; } break; @@ -88,8 +95,7 @@ if (StartLoc.isMacroID() && IgnoreMacros) return; - auto Diag = - diag(StartLoc, "use 'using' instead of 'typedef'"); + auto Diag = diag(StartLoc, "use 'using' instead of 'typedef'"); // do not fix if there is macro or array if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())
Index: clang-tools-extra/test/clang-tidy/modernize-use-using.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/modernize-use-using.cpp +++ clang-tools-extra/test/clang-tidy/modernize-use-using.cpp @@ -182,4 +182,11 @@ class E : public C<E> { void f() override { super::f(); } }; + +template <typename T1, typename T2> +class TwoArgTemplate { + typedef TwoArgTemplate<T1, T2> self; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use 'using' instead of 'typedef' + // CHECK-FIXES: using self = TwoArgTemplate<T1, T2>; +}; } Index: clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp +++ clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp @@ -40,6 +40,7 @@ Token Tok; int ParenLevel = 0; + int AngleBracketLevel = 0; bool FoundTypedef = false; while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) { @@ -54,10 +55,16 @@ case tok::r_paren: ParenLevel--; break; + case tok::less: // '<', start template + AngleBracketLevel++; + break; + case tok::greater: // '>', end template + AngleBracketLevel--; + break; case tok::comma: - if (ParenLevel == 0) { - // If there is comma and we are not between open parenthesis then it is - // two or more declarations in this chain. + if (ParenLevel == 0 && AngleBracketLevel == 0) { + // If there is comma and we are not between open parenthesis or between + // open angle brackets then it is two or more declarations in this chain. return false; } break; @@ -88,8 +95,7 @@ if (StartLoc.isMacroID() && IgnoreMacros) return; - auto Diag = - diag(StartLoc, "use 'using' instead of 'typedef'"); + auto Diag = diag(StartLoc, "use 'using' instead of 'typedef'"); // do not fix if there is macro or array if (MatchedDecl->getUnderlyingType()->isArrayType() || StartLoc.isMacroID())
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits