You've already forked linux-packaging-mono
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
clang-tidy-vs
clangd
docs
include-fixer
modularize
pp-trace
test
tool-template
unittests
change-namespace
clang-apply-replacements
clang-move
clang-query
clang-tidy
CMakeLists.txt
ClangTidyDiagnosticConsumerTest.cpp
ClangTidyOptionsTest.cpp
ClangTidyTest.h
GoogleModuleTest.cpp
IncludeInserterTest.cpp
LLVMModuleTest.cpp
NamespaceAliaserTest.cpp
ObjCModuleTest.cpp
OverlappingReplacementsTest.cpp
ReadabilityModuleTest.cpp
UsingInserterTest.cpp
clangd
include
include-fixer
CMakeLists.txt
.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
115 lines
4.2 KiB
C++
115 lines
4.2 KiB
C++
![]() |
//===---- UsingInserterTest.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 "../clang-tidy/utils/UsingInserter.h"
|
||
|
|
||
|
#include "ClangTidyTest.h"
|
||
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
||
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
||
|
#include "gtest/gtest.h"
|
||
|
|
||
|
namespace clang {
|
||
|
namespace tidy {
|
||
|
namespace utils {
|
||
|
|
||
|
// Replace all function calls with calls to foo::func. Inserts using
|
||
|
// declarations as necessary. This checker is for testing only. It
|
||
|
// can only run on one test case (e.g. wih one SourceManager).
|
||
|
class InsertUsingCheck : public clang::tidy::ClangTidyCheck {
|
||
|
public:
|
||
|
InsertUsingCheck(StringRef Name, ClangTidyContext *Context)
|
||
|
:ClangTidyCheck(Name, Context) {}
|
||
|
void registerMatchers(clang::ast_matchers::MatchFinder *Finder) override {
|
||
|
Finder->addMatcher(clang::ast_matchers::callExpr().bind("foo"), this);
|
||
|
}
|
||
|
void
|
||
|
check(const clang::ast_matchers::MatchFinder::MatchResult &Result) override {
|
||
|
if (!Inserter)
|
||
|
Inserter.reset(new UsingInserter(*Result.SourceManager));
|
||
|
|
||
|
const auto *Call = Result.Nodes.getNodeAs<clang::CallExpr>("foo");
|
||
|
assert(Call != nullptr && "Did not find node \"foo\"");
|
||
|
auto Hint =
|
||
|
Inserter->createUsingDeclaration(*Result.Context, *Call, "::foo::func");
|
||
|
|
||
|
if (Hint.hasValue())
|
||
|
diag(Call->getLocStart(), "Fix for testing") << Hint.getValue();
|
||
|
|
||
|
diag(Call->getLocStart(), "insert call")
|
||
|
<< clang::FixItHint::CreateReplacement(
|
||
|
Call->getCallee()->getSourceRange(),
|
||
|
Inserter->getShortName(*Result.Context, *Call, "::foo::func"));
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
std::unique_ptr<UsingInserter> Inserter;
|
||
|
};
|
||
|
|
||
|
template <typename Check>
|
||
|
std::string runChecker(StringRef Code, unsigned ExpectedWarningCount) {
|
||
|
std::map<StringRef, StringRef> AdditionalFileContents = {{"foo.h",
|
||
|
"namespace foo {\n"
|
||
|
"namespace bar {\n"
|
||
|
"}\n"
|
||
|
"void func() { }\n"
|
||
|
"}"}};
|
||
|
std::vector<ClangTidyError> errors;
|
||
|
|
||
|
std::string result =
|
||
|
test::runCheckOnCode<Check>(Code, &errors, "foo.cc", None,
|
||
|
ClangTidyOptions(), AdditionalFileContents);
|
||
|
|
||
|
EXPECT_EQ(ExpectedWarningCount, errors.size());
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
TEST(UsingInserterTest, ReusesExisting) {
|
||
|
EXPECT_EQ("#include \"foo.h\"\n"
|
||
|
"namespace {"
|
||
|
"using ::foo::func;\n"
|
||
|
"void f() { func(); }"
|
||
|
"}",
|
||
|
runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
|
||
|
"namespace {"
|
||
|
"using ::foo::func;\n"
|
||
|
"void f() { f(); }"
|
||
|
"}",
|
||
|
1));
|
||
|
}
|
||
|
|
||
|
TEST(UsingInserterTest, ReusesExistingGlobal) {
|
||
|
EXPECT_EQ("#include \"foo.h\"\n"
|
||
|
"using ::foo::func;\n"
|
||
|
"namespace {"
|
||
|
"void f() { func(); }"
|
||
|
"}",
|
||
|
runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
|
||
|
"using ::foo::func;\n"
|
||
|
"namespace {"
|
||
|
"void f() { f(); }"
|
||
|
"}",
|
||
|
1));
|
||
|
}
|
||
|
|
||
|
TEST(UsingInserterTest, AvoidsConflict) {
|
||
|
EXPECT_EQ("#include \"foo.h\"\n"
|
||
|
"namespace {"
|
||
|
"void f() { int func; ::foo::func(); }"
|
||
|
"}",
|
||
|
runChecker<InsertUsingCheck>("#include \"foo.h\"\n"
|
||
|
"namespace {"
|
||
|
"void f() { int func; f(); }"
|
||
|
"}",
|
||
|
1));
|
||
|
}
|
||
|
|
||
|
} // namespace utils
|
||
|
} // namespace tidy
|
||
|
} // namespace clang
|