Skip to content

Commit

Permalink
[clang-tidy] Fix handling of methods with try-statement as a body in …
Browse files Browse the repository at this point in the history
…modernize-use-override

Summary:
Fix generated by modernize-use-override caused syntax error when method
used try-statement as a body. `override` keyword was inserted after last
declaration token which happened to be a `try` keyword.

This fixes PR27119.

Reviewers: ehsan, djasper, alexfh

Reviewed By: alexfh

Subscribers: JDevlieghere, cfe-commits

Tags: #clang-tools-extra

Patch by Paweł Żukowski!

Differential Revision: https://reviews.llvm.org/D30002

llvm-svn: 296598
  • Loading branch information
alexfh committed Mar 1, 2017
1 parent c84ca25 commit eedf7ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 6 additions & 7 deletions clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
Expand Up @@ -147,14 +147,13 @@ void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
// end of the declaration of the function, but prefer to put it on the
// same line as the declaration if the beginning brace for the start of
// the body falls on the next line.
Token LastNonCommentToken;
for (Token T : Tokens) {
if (!T.is(tok::comment)) {
LastNonCommentToken = T;
}
}
InsertLoc = LastNonCommentToken.getEndLoc();
ReplacementText = " override";
auto LastTokenIter = std::prev(Tokens.end());
// When try statement is used instead of compound statement as
// method body - insert override keyword before it.
if (LastTokenIter->is(tok::kw_try))
LastTokenIter = std::prev(LastTokenIter);
InsertLoc = LastTokenIter->getEndLoc();
}

if (!InsertLoc.isValid()) {
Expand Down
14 changes: 14 additions & 0 deletions clang-tools-extra/test/clang-tidy/modernize-use-override.cpp
Expand Up @@ -288,3 +288,17 @@ struct MembersOfSpecializations : public Base2 {
};
template <> void MembersOfSpecializations<3>::a() {}
void ff() { MembersOfSpecializations<3>().a(); };

// In case try statement is used as a method body,
// make sure that override fix is placed before try keyword.
struct TryStmtAsBody : public Base {
void a() try
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: annotate this
// CHECK-FIXES: {{^}} void a() override try
{ b(); } catch(...) { c(); }

virtual void d() try
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using
// CHECK-FIXES: {{^}} void d() override try
{ e(); } catch(...) { f(); }
};

0 comments on commit eedf7ec

Please sign in to comment.