diff options
| author | Alexander Kornienko <alexfh@google.com> | 2015-02-27 16:50:32 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2015-02-27 16:50:32 +0000 |
| commit | dd836f238d05aaebdefc108bce41616f336ba3fd (patch) | |
| tree | 9899ff6238e18ed085277a2a4bee79b33457585a /clang-tools-extra/clang-tidy/misc | |
| parent | 6bdd9b060801f1aa46cddfc307e52ae5ff104fb1 (diff) | |
| download | bcm5719-llvm-dd836f238d05aaebdefc108bce41616f336ba3fd.tar.gz bcm5719-llvm-dd836f238d05aaebdefc108bce41616f336ba3fd.zip | |
[clang-tidy] Various improvements in misc-use-override
* Better error message when more than one of 'virtual', 'override' and 'final'
is present ("X is/are redundant since the function is already declared Y").
* Convert the messages to the style used in Clang diagnostics: lower case
initial letter, no trailing period.
* Don't run the check for files compiled in pre-C++11 mode
(http://llvm.org/PR22638).
llvm-svn: 230765
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc')
| -rw-r--r-- | clang-tools-extra/clang-tidy/misc/UseOverride.cpp | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/UseOverride.cpp b/clang-tools-extra/clang-tidy/misc/UseOverride.cpp index 04a920b32df..9c3b5b63209 100644 --- a/clang-tools-extra/clang-tidy/misc/UseOverride.cpp +++ b/clang-tools-extra/clang-tidy/misc/UseOverride.cpp @@ -57,6 +57,9 @@ static StringRef GetText(const Token &Tok, const SourceManager &Sources) { } void UseOverride::check(const MatchFinder::MatchResult &Result) { + if (!Result.Context->getLangOpts().CPlusPlus11) + return; + const FunctionDecl *Method = Result.Nodes.getStmtAs<FunctionDecl>("method"); const SourceManager &Sources = *Result.SourceManager; @@ -78,11 +81,26 @@ void UseOverride::check(const MatchFinder::MatchResult &Result) { if (!OnlyVirtualSpecified && KeywordCount == 1) return; // Nothing to do. - DiagnosticBuilder Diag = diag( - Method->getLocation(), - OnlyVirtualSpecified - ? "Prefer using 'override' or (rarely) 'final' instead of 'virtual'" - : "Annotate this function with 'override' or (rarely) 'final'"); + std::string Message; + + if (OnlyVirtualSpecified) { + Message = + "prefer using 'override' or (rarely) 'final' instead of 'virtual'"; + } else if (KeywordCount == 0) { + Message = "annotate this function with 'override' or (rarely) 'final'"; + } else { + StringRef Redundant = + HasVirtual ? (HasOverride && HasFinal ? "'virtual' and 'override' are" + : "'virtual' is") + : "'override' is"; + StringRef Correct = HasFinal ? "'final'" : "'override'"; + + Message = + (llvm::Twine(Redundant) + + " redundant since the function is already declared " + Correct).str(); + } + + DiagnosticBuilder Diag = diag(Method->getLocation(), Message); CharSourceRange FileRange = Lexer::makeFileCharRange( CharSourceRange::getTokenRange(Method->getSourceRange()), Sources, @@ -146,7 +164,7 @@ void UseOverride::check(const MatchFinder::MatchResult &Result) { CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc)); } - if (Method->isVirtualAsWritten()) { + if (HasVirtual) { for (Token Tok : Tokens) { if (Tok.is(tok::kw_virtual)) { Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange( |

