2015-03-09 19:48:54 +08:00
|
|
|
//===--- UseOverrideCheck.cpp - clang-tidy --------------------------------===//
|
2014-05-16 17:30:09 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-05-16 17:30:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-09 19:48:54 +08:00
|
|
|
#include "UseOverrideCheck.h"
|
2014-05-16 17:30:09 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2015-08-31 21:17:43 +08:00
|
|
|
namespace modernize {
|
2014-05-16 17:30:09 +08:00
|
|
|
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
UseOverrideCheck::UseOverrideCheck(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
IgnoreDestructors(Options.get("IgnoreDestructors", false)),
|
|
|
|
OverrideSpelling(Options.get("OverrideSpelling", "override")),
|
|
|
|
FinalSpelling(Options.get("FinalSpelling", "final")) {}
|
|
|
|
|
2019-02-28 22:55:12 +08:00
|
|
|
void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "IgnoreDestructors", IgnoreDestructors);
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
Options.store(Opts, "OverrideSpelling", OverrideSpelling);
|
|
|
|
Options.store(Opts, "FinalSpelling", FinalSpelling);
|
2019-02-28 22:55:12 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 19:48:54 +08:00
|
|
|
void UseOverrideCheck::registerMatchers(MatchFinder *Finder) {
|
2015-08-29 03:27:19 +08:00
|
|
|
// Only register the matcher for C++11.
|
2019-02-28 22:55:12 +08:00
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (IgnoreDestructors)
|
|
|
|
Finder->addMatcher(
|
|
|
|
cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())).bind("method"),
|
|
|
|
this);
|
|
|
|
else
|
2015-09-17 21:31:25 +08:00
|
|
|
Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this);
|
2014-05-16 17:30:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-lex the tokens to get precise locations to insert 'override' and remove
|
|
|
|
// 'virtual'.
|
2015-01-09 21:56:35 +08:00
|
|
|
static SmallVector<Token, 16>
|
|
|
|
ParseTokens(CharSourceRange Range, const MatchFinder::MatchResult &Result) {
|
|
|
|
const SourceManager &Sources = *Result.SourceManager;
|
2014-05-16 17:30:09 +08:00
|
|
|
std::pair<FileID, unsigned> LocInfo =
|
|
|
|
Sources.getDecomposedLoc(Range.getBegin());
|
|
|
|
StringRef File = Sources.getBufferData(LocInfo.first);
|
|
|
|
const char *TokenBegin = File.data() + LocInfo.second;
|
2015-01-09 21:56:35 +08:00
|
|
|
Lexer RawLexer(Sources.getLocForStartOfFile(LocInfo.first),
|
|
|
|
Result.Context->getLangOpts(), File.begin(), TokenBegin,
|
|
|
|
File.end());
|
2014-05-16 17:30:09 +08:00
|
|
|
SmallVector<Token, 16> Tokens;
|
|
|
|
Token Tok;
|
2017-07-07 18:15:24 +08:00
|
|
|
int NestedParens = 0;
|
2014-05-16 17:30:09 +08:00
|
|
|
while (!RawLexer.LexFromRawLexer(Tok)) {
|
2017-07-07 18:15:24 +08:00
|
|
|
if ((Tok.is(tok::semi) || Tok.is(tok::l_brace)) && NestedParens == 0)
|
2014-05-16 17:30:09 +08:00
|
|
|
break;
|
|
|
|
if (Sources.isBeforeInTranslationUnit(Range.getEnd(), Tok.getLocation()))
|
|
|
|
break;
|
2017-07-07 18:15:24 +08:00
|
|
|
if (Tok.is(tok::l_paren))
|
|
|
|
++NestedParens;
|
|
|
|
else if (Tok.is(tok::r_paren))
|
|
|
|
--NestedParens;
|
2015-01-09 21:56:35 +08:00
|
|
|
if (Tok.is(tok::raw_identifier)) {
|
|
|
|
IdentifierInfo &Info = Result.Context->Idents.get(StringRef(
|
|
|
|
Sources.getCharacterData(Tok.getLocation()), Tok.getLength()));
|
|
|
|
Tok.setIdentifierInfo(&Info);
|
|
|
|
Tok.setKind(Info.getTokenID());
|
|
|
|
}
|
2014-05-16 17:30:09 +08:00
|
|
|
Tokens.push_back(Tok);
|
|
|
|
}
|
|
|
|
return Tokens;
|
|
|
|
}
|
|
|
|
|
2014-05-16 21:37:20 +08:00
|
|
|
static StringRef GetText(const Token &Tok, const SourceManager &Sources) {
|
|
|
|
return StringRef(Sources.getCharacterData(Tok.getLocation()),
|
|
|
|
Tok.getLength());
|
2014-05-16 17:30:09 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 19:48:54 +08:00
|
|
|
void UseOverrideCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-12-14 23:29:23 +08:00
|
|
|
const auto *Method = Result.Nodes.getNodeAs<FunctionDecl>("method");
|
2014-05-16 17:30:09 +08:00
|
|
|
const SourceManager &Sources = *Result.SourceManager;
|
|
|
|
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
ASTContext &Context = *Result.Context;
|
|
|
|
|
2014-05-16 17:30:09 +08:00
|
|
|
assert(Method != nullptr);
|
|
|
|
if (Method->getInstantiatedFromMemberFunction() != nullptr)
|
|
|
|
Method = Method->getInstantiatedFromMemberFunction();
|
|
|
|
|
|
|
|
if (Method->isImplicit() || Method->getLocation().isMacroID() ||
|
|
|
|
Method->isOutOfLine())
|
|
|
|
return;
|
|
|
|
|
2014-06-11 20:18:24 +08:00
|
|
|
bool HasVirtual = Method->isVirtualAsWritten();
|
|
|
|
bool HasOverride = Method->getAttr<OverrideAttr>();
|
|
|
|
bool HasFinal = Method->getAttr<FinalAttr>();
|
|
|
|
|
|
|
|
bool OnlyVirtualSpecified = HasVirtual && !HasOverride && !HasFinal;
|
|
|
|
unsigned KeywordCount = HasVirtual + HasOverride + HasFinal;
|
|
|
|
|
|
|
|
if (!OnlyVirtualSpecified && KeywordCount == 1)
|
2014-05-16 17:30:09 +08:00
|
|
|
return; // Nothing to do.
|
|
|
|
|
2015-02-28 00:50:32 +08:00
|
|
|
std::string Message;
|
|
|
|
if (OnlyVirtualSpecified) {
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
Message = "prefer using '%0' or (rarely) '%1' instead of 'virtual'";
|
2015-02-28 00:50:32 +08:00
|
|
|
} else if (KeywordCount == 0) {
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
Message = "annotate this function with '%0' or (rarely) '%1'";
|
2015-02-28 00:50:32 +08:00
|
|
|
} else {
|
|
|
|
StringRef Redundant =
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
HasVirtual ? (HasOverride && HasFinal ? "'virtual' and '%0' are"
|
2015-02-28 00:50:32 +08:00
|
|
|
: "'virtual' is")
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
: "'%0' is";
|
|
|
|
StringRef Correct = HasFinal ? "'%1'" : "'%0'";
|
2015-02-28 00:50:32 +08:00
|
|
|
|
2016-04-04 22:31:36 +08:00
|
|
|
Message = (llvm::Twine(Redundant) +
|
|
|
|
" redundant since the function is already declared " + Correct)
|
|
|
|
.str();
|
2015-02-28 00:50:32 +08:00
|
|
|
}
|
|
|
|
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
auto Diag = diag(Method->getLocation(), Message)
|
|
|
|
<< OverrideSpelling << FinalSpelling;
|
2014-05-16 17:30:09 +08:00
|
|
|
|
2014-06-20 17:12:46 +08:00
|
|
|
CharSourceRange FileRange = Lexer::makeFileCharRange(
|
|
|
|
CharSourceRange::getTokenRange(Method->getSourceRange()), Sources,
|
2016-09-24 10:13:45 +08:00
|
|
|
getLangOpts());
|
2014-05-16 17:30:09 +08:00
|
|
|
|
|
|
|
if (!FileRange.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Instead of re-lexing and looking for specific macros such as
|
|
|
|
// 'ABSTRACT', properly store the location of 'virtual' and '= 0' in each
|
|
|
|
// FunctionDecl.
|
2015-01-09 21:56:35 +08:00
|
|
|
SmallVector<Token, 16> Tokens = ParseTokens(FileRange, Result);
|
2014-05-16 17:30:09 +08:00
|
|
|
|
|
|
|
// Add 'override' on inline declarations that don't already have it.
|
2014-06-11 20:18:24 +08:00
|
|
|
if (!HasFinal && !HasOverride) {
|
2014-05-16 17:30:09 +08:00
|
|
|
SourceLocation InsertLoc;
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
std::string ReplacementText = OverrideSpelling + " ";
|
2016-04-04 22:31:36 +08:00
|
|
|
SourceLocation MethodLoc = Method->getLocation();
|
2014-05-16 17:30:09 +08:00
|
|
|
|
2015-01-09 21:56:35 +08:00
|
|
|
for (Token T : Tokens) {
|
2016-04-04 22:31:36 +08:00
|
|
|
if (T.is(tok::kw___attribute) &&
|
|
|
|
!Sources.isBeforeInTranslationUnit(T.getLocation(), MethodLoc)) {
|
2015-01-09 21:56:35 +08:00
|
|
|
InsertLoc = T.getLocation();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 17:30:09 +08:00
|
|
|
if (Method->hasAttrs()) {
|
2014-05-19 15:23:03 +08:00
|
|
|
for (const clang::Attr *A : Method->getAttrs()) {
|
2016-04-04 22:31:36 +08:00
|
|
|
if (!A->isImplicit() && !A->isInherited()) {
|
2015-01-09 21:56:35 +08:00
|
|
|
SourceLocation Loc =
|
|
|
|
Sources.getExpansionLoc(A->getRange().getBegin());
|
2016-04-04 22:31:36 +08:00
|
|
|
if ((!InsertLoc.isValid() ||
|
|
|
|
Sources.isBeforeInTranslationUnit(Loc, InsertLoc)) &&
|
|
|
|
!Sources.isBeforeInTranslationUnit(Loc, MethodLoc))
|
2014-11-25 18:45:23 +08:00
|
|
|
InsertLoc = Loc;
|
2014-05-16 17:30:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:31:14 +08:00
|
|
|
if (InsertLoc.isInvalid() && Method->doesThisDeclarationHaveABody() &&
|
2015-08-19 10:05:37 +08:00
|
|
|
Method->getBody() && !Method->isDefaulted()) {
|
|
|
|
// For methods with inline definition, add the override keyword at the
|
|
|
|
// 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.
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
ReplacementText = " " + OverrideSpelling;
|
2017-03-01 18:16:36 +08:00
|
|
|
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();
|
2015-08-19 10:05:37 +08:00
|
|
|
}
|
2014-05-16 17:30:09 +08:00
|
|
|
|
|
|
|
if (!InsertLoc.isValid()) {
|
2015-03-24 06:14:08 +08:00
|
|
|
// For declarations marked with "= 0" or "= [default|delete]", the end
|
|
|
|
// location will point until after those markings. Therefore, the override
|
|
|
|
// keyword shouldn't be inserted at the end, but before the '='.
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
if (Tokens.size() > 2 &&
|
|
|
|
(GetText(Tokens.back(), Sources) == "0" ||
|
|
|
|
Tokens.back().is(tok::kw_default) ||
|
|
|
|
Tokens.back().is(tok::kw_delete)) &&
|
2014-05-16 17:30:09 +08:00
|
|
|
GetText(Tokens[Tokens.size() - 2], Sources) == "=") {
|
|
|
|
InsertLoc = Tokens[Tokens.size() - 2].getLocation();
|
2016-04-04 22:31:36 +08:00
|
|
|
// Check if we need to insert a space.
|
|
|
|
if ((Tokens[Tokens.size() - 2].getFlags() & Token::LeadingSpace) == 0)
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
ReplacementText = " " + OverrideSpelling + " ";
|
|
|
|
} else if (GetText(Tokens.back(), Sources) == "ABSTRACT")
|
2014-05-16 17:30:09 +08:00
|
|
|
InsertLoc = Tokens.back().getLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!InsertLoc.isValid()) {
|
|
|
|
InsertLoc = FileRange.getEnd();
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
ReplacementText = " " + OverrideSpelling;
|
2014-05-16 17:30:09 +08:00
|
|
|
}
|
[clang-tidy] add OverrideMacro to modernize-use-override check
Summary:
The usefulness of **modernize-use-override** can be reduced if you have to live in an environment where you support multiple compilers, some of which sadly are not yet fully C++11 compliant
some codebases have to use override as a macro OVERRIDE e.g.
```
// GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
```
This allows code to be compiled with C++11 compliant compilers and get warnings and errors that clang, MSVC,gcc can give, while still allowing other legacy pre C++11 compilers to compile the code. This can be an important step towards modernizing C++ code whilst living in a legacy codebase.
When it comes to clang tidy, the use of the **modernize-use-override** is one of the most useful checks, but the messages reported are inaccurate for that codebase if the standard approach is to use the macros OVERRIDE and/or FINAL.
When combined with fix-its that introduce the C++11 override keyword, they become fatal, resulting in the modernize-use-override check being turned off to prevent the introduction of such errors.
This revision, allows the possibility for the replacement **override **to be a macro instead, Allowing the clang-tidy check to be run on both pre and post C++11 code, and allowing fix-its to be applied.
Reviewers: alexfh, JonasToth, hokein, Eugene.Zelenko, aaron.ballman
Reviewed By: alexfh, JonasToth
Subscribers: lewmpk, malcolm.parsons, jdoerfert, xazax.hun, cfe-commits, llvm-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D57087
llvm-svn: 355132
2019-03-01 04:00:48 +08:00
|
|
|
|
|
|
|
// If the override macro has been specified just ensure it exists,
|
|
|
|
// if not don't apply a fixit but keep the warning.
|
|
|
|
if (OverrideSpelling != "override" &&
|
|
|
|
!Context.Idents.get(OverrideSpelling).hasMacroDefinition())
|
|
|
|
return;
|
|
|
|
|
2014-05-16 17:30:09 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(InsertLoc, ReplacementText);
|
|
|
|
}
|
|
|
|
|
2014-06-11 20:18:24 +08:00
|
|
|
if (HasFinal && HasOverride) {
|
|
|
|
SourceLocation OverrideLoc = Method->getAttr<OverrideAttr>()->getLocation();
|
|
|
|
Diag << FixItHint::CreateRemoval(
|
|
|
|
CharSourceRange::getTokenRange(OverrideLoc, OverrideLoc));
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:50:32 +08:00
|
|
|
if (HasVirtual) {
|
2014-05-19 15:23:03 +08:00
|
|
|
for (Token Tok : Tokens) {
|
2015-01-09 21:56:35 +08:00
|
|
|
if (Tok.is(tok::kw_virtual)) {
|
2014-05-16 17:30:09 +08:00
|
|
|
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
|
2014-05-19 15:23:03 +08:00
|
|
|
Tok.getLocation(), Tok.getLocation()));
|
2014-05-16 17:30:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:17:43 +08:00
|
|
|
} // namespace modernize
|
2014-05-16 17:30:09 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|