Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
change-namespace
clang-apply-replacements
clang-move
clang-query
clang-reorder-fields
clang-tidy
android
boost
bugprone
cert
cppcoreguidelines
fuchsia
google
hicpp
llvm
misc
modernize
AvoidBindCheck.cpp
AvoidBindCheck.h
CMakeLists.txt
DeprecatedHeadersCheck.cpp
DeprecatedHeadersCheck.h
LoopConvertCheck.cpp
LoopConvertCheck.h
LoopConvertUtils.cpp
LoopConvertUtils.h
MakeSharedCheck.cpp
MakeSharedCheck.h
MakeSmartPtrCheck.cpp
MakeSmartPtrCheck.h
MakeUniqueCheck.cpp
MakeUniqueCheck.h
ModernizeTidyModule.cpp
PassByValueCheck.cpp
PassByValueCheck.h
RawStringLiteralCheck.cpp
RawStringLiteralCheck.h
RedundantVoidArgCheck.cpp
RedundantVoidArgCheck.h
ReplaceAutoPtrCheck.cpp
ReplaceAutoPtrCheck.h
ReplaceRandomShuffleCheck.cpp
ReplaceRandomShuffleCheck.h
ReturnBracedInitListCheck.cpp
ReturnBracedInitListCheck.h
ShrinkToFitCheck.cpp
ShrinkToFitCheck.h
UnaryStaticAssertCheck.cpp
UnaryStaticAssertCheck.h
UseAutoCheck.cpp
UseAutoCheck.h
UseBoolLiteralsCheck.cpp
UseBoolLiteralsCheck.h
UseDefaultMemberInitCheck.cpp
UseDefaultMemberInitCheck.h
UseEmplaceCheck.cpp
UseEmplaceCheck.h
UseEqualsDefaultCheck.cpp
UseEqualsDefaultCheck.h
UseEqualsDeleteCheck.cpp
UseEqualsDeleteCheck.h
UseNoexceptCheck.cpp
UseNoexceptCheck.h
UseNullptrCheck.cpp
UseNullptrCheck.h
UseOverrideCheck.cpp
UseOverrideCheck.h
UseTransparentFunctorsCheck.cpp
UseTransparentFunctorsCheck.h
UseUsingCheck.cpp
UseUsingCheck.h
mpi
objc
performance
plugin
readability
tool
utils
CMakeLists.txt
ClangTidy.cpp
ClangTidy.h
ClangTidyDiagnosticConsumer.cpp
ClangTidyDiagnosticConsumer.h
ClangTidyModule.cpp
ClangTidyModule.h
ClangTidyModuleRegistry.h
ClangTidyOptions.cpp
ClangTidyOptions.h
add_new_check.py
rename_check.py
clang-tidy-vs
clangd
docs
include-fixer
modularize
pp-trace
test
tool-template
unittests
.arcconfig
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
LICENSE.TXT
README.txt
compiler-rt
libcxx
libcxxabi
libunwind
lld
lldb
llvm
openmp
polly
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h

114 lines
3.4 KiB
C++
Raw Normal View History

//===--- UseUsingCheck.cpp - clang-tidy------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "UseUsingCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace modernize {
UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus11)
return;
Finder->addMatcher(typedefDecl().bind("typedef"), this);
}
// 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,
ASTContext &Context) {
assert(StartLoc.isFileID() && "StartLoc must not be in a macro");
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(StartLoc);
StringRef File = SM.getBufferData(LocInfo.first);
const char *TokenBegin = File.data() + LocInfo.second;
Lexer DeclLexer(SM.getLocForStartOfFile(LocInfo.first), Context.getLangOpts(),
File.begin(), TokenBegin, File.end());
Token Tok;
int ParenLevel = 0;
bool FoundTypedef = false;
while (!DeclLexer.LexFromRawLexer(Tok) && !Tok.is(tok::semi)) {
switch (Tok.getKind()) {
case tok::l_brace:
case tok::r_brace:
// This might be the `typedef struct {...} T;` case.
return false;
case tok::l_paren:
ParenLevel++;
break;
case tok::r_paren:
ParenLevel--;
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.
return false;
}
break;
case tok::raw_identifier:
if (Tok.getRawIdentifier() == "typedef") {
FoundTypedef = true;
}
break;
default:
break;
}
}
// Sanity check against weird macro cases.
return FoundTypedef;
}
void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<TypedefDecl>("typedef");
if (MatchedDecl->getLocation().isInvalid())
return;
auto &Context = *Result.Context;
auto &SM = *Result.SourceManager;
SourceLocation StartLoc = MatchedDecl->getLocStart();
if (StartLoc.isMacroID() && IgnoreMacros)
return;
auto Diag =
diag(StartLoc, "use 'using' instead of 'typedef'");
// 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(printPolicy));
}
}
} // namespace modernize
} // namespace tidy
} // namespace clang