Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,16 @@
add_custom_target(ExtraToolsUnitTests)
set_target_properties(ExtraToolsUnitTests PROPERTIES FOLDER "Extra Tools Unit Tests")
function(add_extra_unittest test_dirname)
add_unittest(ExtraToolsUnitTests ${test_dirname} ${ARGN})
endfunction()
add_subdirectory(change-namespace)
add_subdirectory(clang-apply-replacements)
add_subdirectory(clang-move)
add_subdirectory(clang-query)
if(CLANG_ENABLE_STATIC_ANALYZER)
add_subdirectory(clang-tidy)
endif()
add_subdirectory(clangd)
add_subdirectory(include-fixer)

View File

@@ -0,0 +1,29 @@
set(LLVM_LINK_COMPONENTS
support
)
get_filename_component(CHANGE_NAMESPACE_SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/../../change-namespace REALPATH)
include_directories(
${CHANGE_NAMESPACE_SOURCE_DIR}
)
# We'd like clang/unittests/Tooling/RewriterTestContext.h in the test.
include_directories(${CLANG_SOURCE_DIR})
add_extra_unittest(ChangeNamespaceTests
ChangeNamespaceTests.cpp
)
target_link_libraries(ChangeNamespaceTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
clangChangeNamespace
clangFormat
clangFrontend
clangRewrite
clangTooling
clangToolingCore
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,53 @@
//===- clang-apply-replacements/ApplyReplacementsTest.cpp
//----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang-apply-replacements/Tooling/ApplyReplacements.h"
#include "gtest/gtest.h"
using namespace clang::replace;
using namespace llvm;
namespace clang {
namespace tooling {
static TUDiagnostics
makeTUDiagnostics(const std::string &MainSourceFile, StringRef DiagnosticName,
const DiagnosticMessage &Message,
const StringMap<Replacements> &Replacements,
StringRef BuildDirectory) {
TUDiagnostics TUs;
TUs.push_back({MainSourceFile,
{{DiagnosticName,
Message,
Replacements,
{},
Diagnostic::Warning,
BuildDirectory}}});
return TUs;
}
// Test to ensure diagnostics with no fixes, will be merged correctly
// before applying.
TEST(ApplyReplacementsTest, mergeDiagnosticsWithNoFixes) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions());
DiagnosticsEngine Diagnostics(
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), DiagOpts.get());
FileManager Files((FileSystemOptions()));
SourceManager SM(Diagnostics, Files);
TUDiagnostics TUs =
makeTUDiagnostics("path/to/source.cpp", "diagnostic", {}, {}, "path/to");
FileToReplacementsMap ReplacementsMap;
EXPECT_TRUE(mergeAndDeduplicate(TUs, ReplacementsMap, SM));
EXPECT_TRUE(ReplacementsMap.empty());
}
} // end namespace tooling
} // end namespace clang

View File

@@ -0,0 +1,20 @@
get_filename_component(ClangApplyReplacementsLocation
"${CMAKE_CURRENT_SOURCE_DIR}/../../clang-apply-replacements/include" REALPATH)
get_filename_component(CommonIncLocation
"${CMAKE_CURRENT_SOURCE_DIR}/../include" REALPATH)
include_directories(
${ClangApplyReplacementsLocation}
${CommonIncLocation}
)
add_extra_unittest(ClangApplyReplacementsTests
ApplyReplacementsTest.cpp
ReformattingTest.cpp
)
target_link_libraries(ClangApplyReplacementsTests
PRIVATE
clangApplyReplacements
clangBasic
clangToolingCore
)

View File

@@ -0,0 +1,67 @@
//===- clang-apply-replacements/ReformattingTest.cpp ----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang-apply-replacements/Tooling/ApplyReplacements.h"
#include "common/VirtualFileHelper.h"
#include "clang/Format/Format.h"
#include "clang/Tooling/Refactoring.h"
#include "gtest/gtest.h"
using namespace clang;
using namespace clang::tooling;
using namespace clang::replace;
typedef std::vector<clang::tooling::Replacement> ReplacementsVec;
static Replacement makeReplacement(unsigned Offset, unsigned Length,
unsigned ReplacementLength,
llvm::StringRef FilePath = "") {
return Replacement(FilePath, Offset, Length,
std::string(ReplacementLength, '~'));
}
// generate a set of replacements containing one element
static ReplacementsVec makeReplacements(unsigned Offset, unsigned Length,
unsigned ReplacementLength,
llvm::StringRef FilePath = "~") {
ReplacementsVec Replaces;
Replaces.push_back(
makeReplacement(Offset, Length, ReplacementLength, FilePath));
return Replaces;
}
// Put these functions in the clang::tooling namespace so arg-dependent name
// lookup finds these functions for the EXPECT_EQ macros below.
namespace clang {
namespace tooling {
std::ostream &operator<<(std::ostream &os, const Range &R) {
return os << "Range(" << R.getOffset() << ", " << R.getLength() << ")";
}
} // namespace tooling
} // namespace clang
// Ensure zero-length ranges are produced. Even lines where things are deleted
// need reformatting.
TEST(CalculateChangedRangesTest, producesZeroLengthRange) {
RangeVector Changes = calculateChangedRanges(makeReplacements(0, 4, 0));
EXPECT_EQ(Range(0, 0), Changes.front());
}
// Basic test to ensure replacements turn into ranges properly.
TEST(CalculateChangedRangesTest, calculatesRanges) {
ReplacementsVec R;
R.push_back(makeReplacement(2, 0, 3));
R.push_back(makeReplacement(5, 2, 4));
RangeVector Changes = calculateChangedRanges(R);
Range ExpectedRanges[] = { Range(2, 3), Range(8, 4) };
EXPECT_TRUE(std::equal(Changes.begin(), Changes.end(), ExpectedRanges));
}

View File

@@ -0,0 +1,29 @@
set(LLVM_LINK_COMPONENTS
support
)
get_filename_component(INCLUDE_FIXER_SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/../../clang-move REALPATH)
include_directories(
${INCLUDE_FIXER_SOURCE_DIR}
)
# We'd like to clang/unittests/Tooling/RewriterTestContext.h in the test.
include_directories(${CLANG_SOURCE_DIR})
add_extra_unittest(ClangMoveTests
ClangMoveTests.cpp
)
target_link_libraries(ClangMoveTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
clangFormat
clangFrontend
clangMove
clangRewrite
clangTooling
clangToolingCore
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,23 @@
set(LLVM_LINK_COMPONENTS
support
)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../../clang-query
)
add_extra_unittest(ClangQueryTests
QueryEngineTest.cpp
QueryParserTest.cpp
)
target_link_libraries(ClangQueryTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
clangDynamicASTMatchers
clangFrontend
clangQuery
clangTooling
)

View File

@@ -0,0 +1,146 @@
//===---- QueryTest.cpp - clang-query test --------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Query.h"
#include "QueryParser.h"
#include "QuerySession.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include <string>
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::ast_matchers::dynamic;
using namespace clang::query;
using namespace clang::tooling;
class QueryEngineTest : public ::testing::Test {
ArrayRef<std::unique_ptr<ASTUnit>> mkASTUnit2(std::unique_ptr<ASTUnit> a,
std::unique_ptr<ASTUnit> b) {
ASTs[0] = std::move(a);
ASTs[1] = std::move(b);
return ArrayRef<std::unique_ptr<ASTUnit>>(ASTs);
}
protected:
QueryEngineTest()
: S(mkASTUnit2(buildASTFromCode("void foo1(void) {}\nvoid foo2(void) {}",
"foo.cc"),
buildASTFromCode("void bar1(void) {}\nvoid bar2(void) {}",
"bar.cc"))),
OS(Str) {}
std::unique_ptr<ASTUnit> ASTs[2];
QuerySession S;
std::string Str;
llvm::raw_string_ostream OS;
};
TEST_F(QueryEngineTest, Basic) {
DynTypedMatcher FnMatcher = functionDecl();
DynTypedMatcher FooMatcher = functionDecl(hasName("foo1"));
EXPECT_TRUE(NoOpQuery().run(OS, S));
EXPECT_EQ("", OS.str());
Str.clear();
EXPECT_FALSE(InvalidQuery("Parse error").run(OS, S));
EXPECT_EQ("Parse error\n", OS.str());
Str.clear();
EXPECT_TRUE(HelpQuery().run(OS, S));
EXPECT_TRUE(OS.str().find("Available commands:") != std::string::npos);
Str.clear();
EXPECT_TRUE(MatchQuery(FnMatcher).run(OS, S));
EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") !=
std::string::npos);
EXPECT_TRUE(OS.str().find("foo.cc:2:1: note: \"root\" binds here") !=
std::string::npos);
EXPECT_TRUE(OS.str().find("bar.cc:1:1: note: \"root\" binds here") !=
std::string::npos);
EXPECT_TRUE(OS.str().find("bar.cc:2:1: note: \"root\" binds here") !=
std::string::npos);
EXPECT_TRUE(OS.str().find("4 matches.") != std::string::npos);
Str.clear();
EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S));
EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") !=
std::string::npos);
EXPECT_TRUE(OS.str().find("1 match.") != std::string::npos);
Str.clear();
EXPECT_TRUE(
SetQuery<OutputKind>(&QuerySession::OutKind, OK_Print).run(OS, S));
EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S));
EXPECT_TRUE(OS.str().find("Binding for \"root\":\nvoid foo1()") !=
std::string::npos);
Str.clear();
EXPECT_TRUE(SetQuery<OutputKind>(&QuerySession::OutKind, OK_Dump).run(OS, S));
EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S));
EXPECT_TRUE(OS.str().find("FunctionDecl") != std::string::npos);
Str.clear();
EXPECT_TRUE(SetQuery<bool>(&QuerySession::BindRoot, false).run(OS, S));
EXPECT_TRUE(MatchQuery(FooMatcher).run(OS, S));
EXPECT_TRUE(OS.str().find("No bindings.") != std::string::npos);
Str.clear();
EXPECT_FALSE(MatchQuery(isArrow()).run(OS, S));
EXPECT_EQ("Not a valid top-level matcher.\n", OS.str());
}
TEST_F(QueryEngineTest, LetAndMatch) {
EXPECT_TRUE(QueryParser::parse("let x \"foo1\"", S)->run(OS, S));
EXPECT_EQ("", OS.str());
Str.clear();
EXPECT_TRUE(QueryParser::parse("let y hasName(x)", S)->run(OS, S));
EXPECT_EQ("", OS.str());
Str.clear();
EXPECT_TRUE(QueryParser::parse("match functionDecl(y)", S)->run(OS, S));
EXPECT_TRUE(OS.str().find("foo.cc:1:1: note: \"root\" binds here") !=
std::string::npos);
EXPECT_TRUE(OS.str().find("1 match.") != std::string::npos);
Str.clear();
EXPECT_TRUE(QueryParser::parse("unlet x", S)->run(OS, S));
EXPECT_EQ("", OS.str());
Str.clear();
EXPECT_FALSE(QueryParser::parse("let y hasName(x)", S)->run(OS, S));
EXPECT_EQ("1:2: Error parsing argument 1 for matcher hasName.\n"
"1:10: Value not found: x\n", OS.str());
Str.clear();
}

View File

@@ -0,0 +1,158 @@
//===---- QueryParserTest.cpp - clang-query test --------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "QueryParser.h"
#include "Query.h"
#include "QuerySession.h"
#include "llvm/LineEditor/LineEditor.h"
#include "gtest/gtest.h"
using namespace clang;
using namespace clang::query;
class QueryParserTest : public ::testing::Test {
protected:
QueryParserTest() : QS(llvm::ArrayRef<std::unique_ptr<ASTUnit>>()) {}
QueryRef parse(StringRef Code) { return QueryParser::parse(Code, QS); }
QuerySession QS;
};
TEST_F(QueryParserTest, NoOp) {
QueryRef Q = parse("");
EXPECT_TRUE(isa<NoOpQuery>(Q));
Q = parse("\n");
EXPECT_TRUE(isa<NoOpQuery>(Q));
}
TEST_F(QueryParserTest, Invalid) {
QueryRef Q = parse("foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("unknown command: foo", cast<InvalidQuery>(Q)->ErrStr);
}
TEST_F(QueryParserTest, Help) {
QueryRef Q = parse("help");
ASSERT_TRUE(isa<HelpQuery>(Q));
Q = parse("help me");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("unexpected extra input: ' me'", cast<InvalidQuery>(Q)->ErrStr);
}
TEST_F(QueryParserTest, Set) {
QueryRef Q = parse("set");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set foo bar");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("unknown variable: 'foo'", cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set output");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected 'diag', 'print' or 'dump', got ''",
cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set bind-root true foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("unexpected extra input: ' foo'", cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set output foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected 'diag', 'print' or 'dump', got 'foo'",
cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set output dump");
ASSERT_TRUE(isa<SetQuery<OutputKind> >(Q));
EXPECT_EQ(&QuerySession::OutKind, cast<SetQuery<OutputKind> >(Q)->Var);
EXPECT_EQ(OK_Dump, cast<SetQuery<OutputKind> >(Q)->Value);
Q = parse("set bind-root foo");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected 'true' or 'false', got 'foo'",
cast<InvalidQuery>(Q)->ErrStr);
Q = parse("set bind-root true");
ASSERT_TRUE(isa<SetQuery<bool> >(Q));
EXPECT_EQ(&QuerySession::BindRoot, cast<SetQuery<bool> >(Q)->Var);
EXPECT_EQ(true, cast<SetQuery<bool> >(Q)->Value);
}
TEST_F(QueryParserTest, Match) {
QueryRef Q = parse("match decl()");
ASSERT_TRUE(isa<MatchQuery>(Q));
EXPECT_TRUE(cast<MatchQuery>(Q)->Matcher.canConvertTo<Decl>());
Q = parse("m stmt()");
ASSERT_TRUE(isa<MatchQuery>(Q));
EXPECT_TRUE(cast<MatchQuery>(Q)->Matcher.canConvertTo<Stmt>());
}
TEST_F(QueryParserTest, LetUnlet) {
QueryRef Q = parse("let foo decl()");
ASSERT_TRUE(isa<LetQuery>(Q));
EXPECT_EQ("foo", cast<LetQuery>(Q)->Name);
EXPECT_TRUE(cast<LetQuery>(Q)->Value.isMatcher());
EXPECT_TRUE(cast<LetQuery>(Q)->Value.getMatcher().hasTypedMatcher<Decl>());
Q = parse("let bar \"str\"");
ASSERT_TRUE(isa<LetQuery>(Q));
EXPECT_EQ("bar", cast<LetQuery>(Q)->Name);
EXPECT_TRUE(cast<LetQuery>(Q)->Value.isString());
EXPECT_EQ("str", cast<LetQuery>(Q)->Value.getString());
Q = parse("let");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
Q = parse("unlet x");
ASSERT_TRUE(isa<LetQuery>(Q));
EXPECT_EQ("x", cast<LetQuery>(Q)->Name);
EXPECT_FALSE(cast<LetQuery>(Q)->Value.hasValue());
Q = parse("unlet");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
Q = parse("unlet x bad_data");
ASSERT_TRUE(isa<InvalidQuery>(Q));
EXPECT_EQ("unexpected extra input: ' bad_data'",
cast<InvalidQuery>(Q)->ErrStr);
}
TEST_F(QueryParserTest, Complete) {
std::vector<llvm::LineEditor::Completion> Comps =
QueryParser::complete("", 0, QS);
ASSERT_EQ(6u, Comps.size());
EXPECT_EQ("help ", Comps[0].TypedText);
EXPECT_EQ("help", Comps[0].DisplayText);
EXPECT_EQ("let ", Comps[1].TypedText);
EXPECT_EQ("let", Comps[1].DisplayText);
EXPECT_EQ("match ", Comps[2].TypedText);
EXPECT_EQ("match", Comps[2].DisplayText);
EXPECT_EQ("set ", Comps[3].TypedText);
EXPECT_EQ("set", Comps[3].DisplayText);
EXPECT_EQ("unlet ", Comps[4].TypedText);
EXPECT_EQ("unlet", Comps[4].DisplayText);
EXPECT_EQ("quit", Comps[5].DisplayText);
EXPECT_EQ("quit ", Comps[5].TypedText);
Comps = QueryParser::complete("set o", 5, QS);
ASSERT_EQ(1u, Comps.size());
EXPECT_EQ("utput ", Comps[0].TypedText);
EXPECT_EQ("output", Comps[0].DisplayText);
Comps = QueryParser::complete("match while", 11, QS);
ASSERT_EQ(1u, Comps.size());
EXPECT_EQ("Stmt(", Comps[0].TypedText);
EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
Comps[0].DisplayText);
}

View File

@@ -0,0 +1,37 @@
set(LLVM_LINK_COMPONENTS
support
)
get_filename_component(CLANG_LINT_SOURCE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/../../clang-tidy REALPATH)
include_directories(${CLANG_LINT_SOURCE_DIR})
add_extra_unittest(ClangTidyTests
ClangTidyDiagnosticConsumerTest.cpp
ClangTidyOptionsTest.cpp
IncludeInserterTest.cpp
GoogleModuleTest.cpp
LLVMModuleTest.cpp
NamespaceAliaserTest.cpp
ObjCModuleTest.cpp
OverlappingReplacementsTest.cpp
UsingInserterTest.cpp
ReadabilityModuleTest.cpp)
target_link_libraries(ClangTidyTests
PRIVATE
clangAST
clangASTMatchers
clangBasic
clangFrontend
clangLex
clangTidy
clangTidyAndroidModule
clangTidyGoogleModule
clangTidyLLVMModule
clangTidyObjCModule
clangTidyReadabilityModule
clangTidyUtils
clangTooling
clangToolingCore
)

View File

@@ -0,0 +1,94 @@
#include "ClangTidy.h"
#include "ClangTidyTest.h"
#include "gtest/gtest.h"
namespace clang {
namespace tidy {
namespace test {
class TestCheck : public ClangTidyCheck {
public:
TestCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override {
Finder->addMatcher(ast_matchers::varDecl().bind("var"), this);
}
void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
// Add diagnostics in the wrong order.
diag(Var->getLocation(), "variable");
diag(Var->getTypeSpecStartLoc(), "type specifier");
}
};
TEST(ClangTidyDiagnosticConsumer, SortsErrors) {
std::vector<ClangTidyError> Errors;
runCheckOnCode<TestCheck>("int a;", &Errors);
EXPECT_EQ(2ul, Errors.size());
EXPECT_EQ("type specifier", Errors[0].Message.Message);
EXPECT_EQ("variable", Errors[1].Message.Message);
}
TEST(GlobList, Empty) {
GlobList Filter("");
EXPECT_TRUE(Filter.contains(""));
EXPECT_FALSE(Filter.contains("aaa"));
}
TEST(GlobList, Nothing) {
GlobList Filter("-*");
EXPECT_FALSE(Filter.contains(""));
EXPECT_FALSE(Filter.contains("a"));
EXPECT_FALSE(Filter.contains("-*"));
EXPECT_FALSE(Filter.contains("-"));
EXPECT_FALSE(Filter.contains("*"));
}
TEST(GlobList, Everything) {
GlobList Filter("*");
EXPECT_TRUE(Filter.contains(""));
EXPECT_TRUE(Filter.contains("aaaa"));
EXPECT_TRUE(Filter.contains("-*"));
EXPECT_TRUE(Filter.contains("-"));
EXPECT_TRUE(Filter.contains("*"));
}
TEST(GlobList, Simple) {
GlobList Filter("aaa");
EXPECT_TRUE(Filter.contains("aaa"));
EXPECT_FALSE(Filter.contains(""));
EXPECT_FALSE(Filter.contains("aa"));
EXPECT_FALSE(Filter.contains("aaaa"));
EXPECT_FALSE(Filter.contains("bbb"));
}
TEST(GlobList, WhitespacesAtBegin) {
GlobList Filter("-*, a.b.*");
EXPECT_TRUE(Filter.contains("a.b.c"));
EXPECT_FALSE(Filter.contains("b.c"));
}
TEST(GlobList, Complex) {
GlobList Filter("*,-a.*, -b.*, \r \n a.1.* ,-a.1.A.*,-..,-...,-..+,-*$, -*qwe* ");
EXPECT_TRUE(Filter.contains("aaa"));
EXPECT_TRUE(Filter.contains("qqq"));
EXPECT_FALSE(Filter.contains("a."));
EXPECT_FALSE(Filter.contains("a.b"));
EXPECT_FALSE(Filter.contains("b."));
EXPECT_FALSE(Filter.contains("b.b"));
EXPECT_TRUE(Filter.contains("a.1.b"));
EXPECT_FALSE(Filter.contains("a.1.A.a"));
EXPECT_FALSE(Filter.contains("qwe"));
EXPECT_FALSE(Filter.contains("asdfqweasdf"));
EXPECT_TRUE(Filter.contains("asdfqwEasdf"));
}
} // namespace test
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,104 @@
#include "ClangTidyOptions.h"
#include "gtest/gtest.h"
#include "llvm/ADT/StringExtras.h"
namespace clang {
namespace tidy {
namespace test {
TEST(ParseLineFilter, EmptyFilter) {
ClangTidyGlobalOptions Options;
EXPECT_FALSE(parseLineFilter("", Options));
EXPECT_TRUE(Options.LineFilter.empty());
EXPECT_FALSE(parseLineFilter("[]", Options));
EXPECT_TRUE(Options.LineFilter.empty());
}
TEST(ParseLineFilter, InvalidFilter) {
ClangTidyGlobalOptions Options;
EXPECT_TRUE(!!parseLineFilter("asdf", Options));
EXPECT_TRUE(Options.LineFilter.empty());
EXPECT_TRUE(!!parseLineFilter("[{}]", Options));
EXPECT_TRUE(!!parseLineFilter("[{\"name\":\"\"}]", Options));
EXPECT_TRUE(
!!parseLineFilter("[{\"name\":\"test\",\"lines\":[[1]]}]", Options));
EXPECT_TRUE(
!!parseLineFilter("[{\"name\":\"test\",\"lines\":[[1,2,3]]}]", Options));
EXPECT_TRUE(
!!parseLineFilter("[{\"name\":\"test\",\"lines\":[[1,-1]]}]", Options));
}
TEST(ParseLineFilter, ValidFilter) {
ClangTidyGlobalOptions Options;
std::error_code Error = parseLineFilter(
"[{\"name\":\"file1.cpp\",\"lines\":[[3,15],[20,30],[42,42]]},"
"{\"name\":\"file2.h\"},"
"{\"name\":\"file3.cc\",\"lines\":[[100,1000]]}]",
Options);
EXPECT_FALSE(Error);
EXPECT_EQ(3u, Options.LineFilter.size());
EXPECT_EQ("file1.cpp", Options.LineFilter[0].Name);
EXPECT_EQ(3u, Options.LineFilter[0].LineRanges.size());
EXPECT_EQ(3u, Options.LineFilter[0].LineRanges[0].first);
EXPECT_EQ(15u, Options.LineFilter[0].LineRanges[0].second);
EXPECT_EQ(20u, Options.LineFilter[0].LineRanges[1].first);
EXPECT_EQ(30u, Options.LineFilter[0].LineRanges[1].second);
EXPECT_EQ(42u, Options.LineFilter[0].LineRanges[2].first);
EXPECT_EQ(42u, Options.LineFilter[0].LineRanges[2].second);
EXPECT_EQ("file2.h", Options.LineFilter[1].Name);
EXPECT_EQ(0u, Options.LineFilter[1].LineRanges.size());
EXPECT_EQ("file3.cc", Options.LineFilter[2].Name);
EXPECT_EQ(1u, Options.LineFilter[2].LineRanges.size());
EXPECT_EQ(100u, Options.LineFilter[2].LineRanges[0].first);
EXPECT_EQ(1000u, Options.LineFilter[2].LineRanges[0].second);
}
TEST(ParseConfiguration, ValidConfiguration) {
llvm::ErrorOr<ClangTidyOptions> Options =
parseConfiguration("Checks: \"-*,misc-*\"\n"
"HeaderFilterRegex: \".*\"\n"
"AnalyzeTemporaryDtors: true\n"
"User: some.user");
EXPECT_TRUE(!!Options);
EXPECT_EQ("-*,misc-*", *Options->Checks);
EXPECT_EQ(".*", *Options->HeaderFilterRegex);
EXPECT_TRUE(*Options->AnalyzeTemporaryDtors);
EXPECT_EQ("some.user", *Options->User);
}
TEST(ParseConfiguration, MergeConfigurations) {
llvm::ErrorOr<ClangTidyOptions> Options1 = parseConfiguration(R"(
Checks: "check1,check2"
HeaderFilterRegex: "filter1"
AnalyzeTemporaryDtors: true
User: user1
ExtraArgs: ['arg1', 'arg2']
ExtraArgsBefore: ['arg-before1', 'arg-before2']
)");
ASSERT_TRUE(!!Options1);
llvm::ErrorOr<ClangTidyOptions> Options2 = parseConfiguration(R"(
Checks: "check3,check4"
HeaderFilterRegex: "filter2"
AnalyzeTemporaryDtors: false
User: user2
ExtraArgs: ['arg3', 'arg4']
ExtraArgsBefore: ['arg-before3', 'arg-before4']
)");
ASSERT_TRUE(!!Options2);
ClangTidyOptions Options = Options1->mergeWith(*Options2);
EXPECT_EQ("check1,check2,check3,check4", *Options.Checks);
EXPECT_EQ("filter2", *Options.HeaderFilterRegex);
EXPECT_FALSE(*Options.AnalyzeTemporaryDtors);
EXPECT_EQ("user2", *Options.User);
ASSERT_TRUE(Options.ExtraArgs.hasValue());
EXPECT_EQ("arg1,arg2,arg3,arg4", llvm::join(Options.ExtraArgs->begin(),
Options.ExtraArgs->end(), ","));
ASSERT_TRUE(Options.ExtraArgsBefore.hasValue());
EXPECT_EQ("arg-before1,arg-before2,arg-before3,arg-before4",
llvm::join(Options.ExtraArgsBefore->begin(),
Options.ExtraArgsBefore->end(), ","));
}
} // namespace test
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,159 @@
//===--- ClangTidyTest.h - clang-tidy ---------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANGTIDYTEST_H
#define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANGTIDYTEST_H
#include "ClangTidy.h"
#include "ClangTidyDiagnosticConsumer.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Path.h"
#include <map>
#include <memory>
namespace clang {
namespace tidy {
namespace test {
class TestClangTidyAction : public ASTFrontendAction {
public:
TestClangTidyAction(SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Checks,
ast_matchers::MatchFinder &Finder,
ClangTidyContext &Context)
: Checks(Checks), Finder(Finder), Context(Context) {}
private:
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
StringRef File) override {
Context.setSourceManager(&Compiler.getSourceManager());
Context.setCurrentFile(File);
Context.setASTContext(&Compiler.getASTContext());
for (auto &Check : Checks) {
Check->registerMatchers(&Finder);
Check->registerPPCallbacks(Compiler);
}
return Finder.newASTConsumer();
}
SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Checks;
ast_matchers::MatchFinder &Finder;
ClangTidyContext &Context;
};
template <typename Check, typename... Checks> struct CheckFactory {
static void
createChecks(ClangTidyContext *Context,
SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Result) {
CheckFactory<Check>::createChecks(Context, Result);
CheckFactory<Checks...>::createChecks(Context, Result);
}
};
template <typename Check> struct CheckFactory<Check> {
static void
createChecks(ClangTidyContext *Context,
SmallVectorImpl<std::unique_ptr<ClangTidyCheck>> &Result) {
Result.emplace_back(llvm::make_unique<Check>(
"test-check-" + std::to_string(Result.size()), Context));
}
};
template <typename... CheckList>
std::string
runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
const Twine &Filename = "input.cc",
ArrayRef<std::string> ExtraArgs = None,
const ClangTidyOptions &ExtraOptions = ClangTidyOptions(),
std::map<StringRef, StringRef> PathsToContent =
std::map<StringRef, StringRef>()) {
ClangTidyOptions Options = ExtraOptions;
Options.Checks = "*";
ClangTidyContext Context(llvm::make_unique<DefaultOptionsProvider>(
ClangTidyGlobalOptions(), Options));
ClangTidyDiagnosticConsumer DiagConsumer(Context);
std::vector<std::string> Args(1, "clang-tidy");
Args.push_back("-fsyntax-only");
std::string extension(llvm::sys::path::extension(Filename.str()));
if (extension == ".m" || extension == ".mm") {
Args.push_back("-fobjc-abi-version=2");
Args.push_back("-fobjc-arc");
}
if (extension == ".cc" || extension == ".cpp" || extension == ".mm") {
Args.push_back("-std=c++11");
}
Args.push_back("-Iinclude");
Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
Args.push_back(Filename.str());
ast_matchers::MatchFinder Finder;
llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
new vfs::InMemoryFileSystem);
llvm::IntrusiveRefCntPtr<FileManager> Files(
new FileManager(FileSystemOptions(), InMemoryFileSystem));
SmallVector<std::unique_ptr<ClangTidyCheck>, 1> Checks;
CheckFactory<CheckList...>::createChecks(&Context, Checks);
tooling::ToolInvocation Invocation(
Args, new TestClangTidyAction(Checks, Finder, Context), Files.get());
InMemoryFileSystem->addFile(Filename, 0,
llvm::MemoryBuffer::getMemBuffer(Code));
for (const auto &FileContent : PathsToContent) {
InMemoryFileSystem->addFile(
Twine("include/") + FileContent.first, 0,
llvm::MemoryBuffer::getMemBuffer(FileContent.second));
}
Invocation.setDiagnosticConsumer(&DiagConsumer);
if (!Invocation.run()) {
std::string ErrorText;
for (const auto &Error : Context.getErrors()) {
ErrorText += Error.Message.Message + "\n";
}
llvm::report_fatal_error(ErrorText);
}
DiagConsumer.finish();
tooling::Replacements Fixes;
for (const ClangTidyError &Error : Context.getErrors()) {
for (const auto &FileAndFixes : Error.Fix) {
for (const auto &Fix : FileAndFixes.second) {
auto Err = Fixes.add(Fix);
// FIXME: better error handling. Keep the behavior for now.
if (Err) {
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
return "";
}
}
}
}
if (Errors)
*Errors = Context.getErrors();
auto Result = tooling::applyAllReplacements(Code, Fixes);
if (!Result) {
// FIXME: propogate the error.
llvm::consumeError(Result.takeError());
return "";
}
return *Result;
}
#define EXPECT_NO_CHANGES(Check, Code) \
EXPECT_EQ(Code, runCheckOnCode<Check>(Code))
} // namespace test
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_TIDY_CLANGTIDYTEST_H

View File

@@ -0,0 +1,110 @@
#include "ClangTidyTest.h"
#include "google/ExplicitConstructorCheck.h"
#include "google/GlobalNamesInHeadersCheck.h"
#include "gtest/gtest.h"
using namespace clang::tidy::google;
namespace clang {
namespace tidy {
namespace test {
TEST(ExplicitConstructorCheckTest, SingleArgumentConstructorsOnly) {
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(); };");
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(int i, int j); };");
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(const C&); };");
EXPECT_NO_CHANGES(ExplicitConstructorCheck, "class C { C(C&&); };");
EXPECT_NO_CHANGES(ExplicitConstructorCheck,
"class C { C(const C&) = delete; };");
EXPECT_NO_CHANGES(ExplicitConstructorCheck,
"class C { C(int) = delete; };");
}
TEST(ExplicitConstructorCheckTest, Basic) {
EXPECT_EQ("class C { explicit C(int i); };",
runCheckOnCode<ExplicitConstructorCheck>("class C { C(int i); };"));
}
TEST(ExplicitConstructorCheckTest, DefaultParameters) {
EXPECT_EQ("class C { explicit C(int i, int j = 0); };",
runCheckOnCode<ExplicitConstructorCheck>(
"class C { C(int i, int j = 0); };"));
}
TEST(ExplicitConstructorCheckTest, OutOfLineDefinitions) {
EXPECT_EQ("class C { explicit C(int i); }; C::C(int i) {}",
runCheckOnCode<ExplicitConstructorCheck>(
"class C { C(int i); }; C::C(int i) {}"));
}
TEST(ExplicitConstructorCheckTest, RemoveExplicit) {
EXPECT_EQ("class A { A(const A&); };\n"
"class B { /*asdf*/ B(B&&); };\n"
"class C { /*asdf*/ C(const C&, int i = 0); };",
runCheckOnCode<ExplicitConstructorCheck>(
"class A { explicit A(const A&); };\n"
"class B { explicit /*asdf*/ B(B&&); };\n"
"class C { explicit/*asdf*/ C(const C&, int i = 0); };"));
}
TEST(ExplicitConstructorCheckTest, RemoveExplicitWithMacros) {
EXPECT_EQ(
"#define A(T) class T##Bar { explicit T##Bar(const T##Bar &b) {} };\n"
"A(Foo);",
runCheckOnCode<ExplicitConstructorCheck>(
"#define A(T) class T##Bar { explicit T##Bar(const T##Bar &b) {} };\n"
"A(Foo);"));
}
class GlobalNamesInHeadersCheckTest : public ::testing::Test {
protected:
bool runCheckOnCode(const std::string &Code, const std::string &Filename) {
static const char *const Header = "namespace std {\n"
"class string {};\n"
"} // namespace std\n"
"\n"
"#define SOME_MACRO(x) using x\n";
std::vector<ClangTidyError> Errors;
std::vector<std::string> Args;
if (!StringRef(Filename).endswith(".cpp")) {
Args.emplace_back("-xc++-header");
}
test::runCheckOnCode<readability::GlobalNamesInHeadersCheck>(
Header + Code, &Errors, Filename, Args);
if (Errors.empty())
return false;
assert(Errors.size() == 1);
assert(
Errors[0].Message.Message ==
"using declarations in the global namespace in headers are prohibited");
return true;
}
};
TEST_F(GlobalNamesInHeadersCheckTest, UsingDeclarations) {
EXPECT_TRUE(runCheckOnCode("using std::string;", "foo.h"));
EXPECT_FALSE(runCheckOnCode("using std::string;", "foo.cpp"));
EXPECT_FALSE(runCheckOnCode("namespace my_namespace {\n"
"using std::string;\n"
"} // my_namespace\n",
"foo.h"));
EXPECT_FALSE(runCheckOnCode("SOME_MACRO(std::string);", "foo.h"));
}
TEST_F(GlobalNamesInHeadersCheckTest, UsingDirectives) {
EXPECT_TRUE(runCheckOnCode("using namespace std;", "foo.h"));
EXPECT_FALSE(runCheckOnCode("using namespace std;", "foo.cpp"));
EXPECT_FALSE(runCheckOnCode("namespace my_namespace {\n"
"using namespace std;\n"
"} // my_namespace\n",
"foo.h"));
EXPECT_FALSE(runCheckOnCode("SOME_MACRO(namespace std);", "foo.h"));
}
TEST_F(GlobalNamesInHeadersCheckTest, RegressionAnonymousNamespace) {
EXPECT_FALSE(runCheckOnCode("namespace {}", "foo.h"));
}
} // namespace test
} // namespace tidy
} // namespace clang

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,219 @@
#include "ClangTidyTest.h"
#include "llvm/HeaderGuardCheck.h"
#include "llvm/IncludeOrderCheck.h"
#include "gtest/gtest.h"
using namespace clang::tidy::llvm;
namespace clang {
namespace tidy {
namespace test {
// FIXME: It seems this might be incompatible to dos path. Investigating.
#if !defined(_WIN32)
static std::string runHeaderGuardCheck(StringRef Code, const Twine &Filename,
Optional<StringRef> ExpectedWarning) {
std::vector<ClangTidyError> Errors;
std::string Result = test::runCheckOnCode<LLVMHeaderGuardCheck>(
Code, &Errors, Filename, std::string("-xc++-header"));
if (Errors.size() != (size_t)ExpectedWarning.hasValue())
return "invalid error count";
if (ExpectedWarning && *ExpectedWarning != Errors.back().Message.Message)
return "expected: '" + ExpectedWarning->str() + "', saw: '" +
Errors.back().Message.Message + "'";
return Result;
}
namespace {
struct WithEndifComment : public LLVMHeaderGuardCheck {
WithEndifComment(StringRef Name, ClangTidyContext *Context)
: LLVMHeaderGuardCheck(Name, Context) {}
bool shouldSuggestEndifComment(StringRef Filename) override { return true; }
};
} // namespace
static std::string
runHeaderGuardCheckWithEndif(StringRef Code, const Twine &Filename,
Optional<StringRef> ExpectedWarning) {
std::vector<ClangTidyError> Errors;
std::string Result = test::runCheckOnCode<WithEndifComment>(
Code, &Errors, Filename, std::string("-xc++-header"));
if (Errors.size() != (size_t)ExpectedWarning.hasValue())
return "invalid error count";
if (ExpectedWarning && *ExpectedWarning != Errors.back().Message.Message)
return "expected: '" + ExpectedWarning->str() + "', saw: '" +
Errors.back().Message.Message + "'";
return Result;
}
TEST(LLVMHeaderGuardCheckTest, FixHeaderGuards) {
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif\n",
runHeaderGuardCheck(
"#ifndef FOO\n"
"#define FOO\n"
"#endif\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
// Allow trailing underscores.
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif\n",
runHeaderGuardCheck("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif\n",
"include/llvm/ADT/foo.h", None));
EXPECT_EQ("#ifndef LLVM_CLANG_C_BAR_H\n"
"#define LLVM_CLANG_C_BAR_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "./include/clang-c/bar.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_LIB_CODEGEN_C_H\n"
"#define LLVM_CLANG_LIB_CODEGEN_C_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "tools/clang/lib/CodeGen/c.h",
StringRef("header is missing header guard")));
EXPECT_EQ("#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n"
"#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_X_H\n"
"\n"
"\n"
"#endif\n",
runHeaderGuardCheck("", "tools/clang/tools/extra/clang-tidy/x.h",
StringRef("header is missing header guard")));
EXPECT_EQ(
"int foo;\n"
"#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n",
runHeaderGuardCheck("int foo;\n"
"#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n",
"include/clang/bar.h",
StringRef("code/includes outside of area guarded by "
"header guard; consider moving it")));
EXPECT_EQ(
"#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n"
"int foo;\n",
runHeaderGuardCheck("#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"#endif\n"
"int foo;\n",
"include/clang/bar.h",
StringRef("code/includes outside of area guarded by "
"header guard; consider moving it")));
EXPECT_EQ("#ifndef LLVM_CLANG_BAR_H\n"
"#define LLVM_CLANG_BAR_H\n"
"\n"
"int foo;\n"
"#ifndef FOOLOLO\n"
"#define FOOLOLO\n"
"#endif\n"
"\n"
"#endif\n",
runHeaderGuardCheck("int foo;\n"
"#ifndef FOOLOLO\n"
"#define FOOLOLO\n"
"#endif\n",
"include/clang/bar.h",
StringRef("header is missing header guard")));
// Fix incorrect #endif comments even if we shouldn't add new ones.
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheck(
"#ifndef FOO\n"
"#define FOO\n"
"#endif // FOO\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef FOO\n"
"#define FOO\n"
"#endif\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_H\n",
"include/llvm/ADT/foo.h",
StringRef("#endif for a header guard should reference the "
"guard macro in a comment")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H */\n",
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H */\n",
"include/llvm/ADT/foo.h", None));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif // LLVM_ADT_FOO_H_\n",
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif // LLVM_ADT_FOO_H_\n",
"include/llvm/ADT/foo.h", None));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif // LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef LLVM_ADT_FOO_H_\n"
"#define LLVM_ADT_FOO_H_\n"
"#endif // LLVM\n",
"include/llvm/ADT/foo.h",
StringRef("header guard does not follow preferred style")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif \\ \n"
"// LLVM_ADT_FOO_H\n",
runHeaderGuardCheckWithEndif(
"#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif \\ \n"
"// LLVM_ADT_FOO_H\n",
"include/llvm/ADT/foo.h",
StringRef("backslash and newline separated by space")));
EXPECT_EQ("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H\\ \n"
" FOO */",
runHeaderGuardCheckWithEndif("#ifndef LLVM_ADT_FOO_H\n"
"#define LLVM_ADT_FOO_H\n"
"#endif /* LLVM_ADT_FOO_H\\ \n"
" FOO */",
"include/llvm/ADT/foo.h", None));
}
#endif
} // namespace test
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,123 @@
//===---- NamespaceAliaserTest.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/NamespaceAliaser.h"
#include "ClangTidyTest.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "gtest/gtest.h"
namespace clang {
namespace tidy {
namespace utils {
// This checker is for testing only. It can only run on one test case
// (e.g. with one SourceManager).
class InsertAliasCheck : public ClangTidyCheck {
public:
InsertAliasCheck(StringRef Name, ClangTidyContext *Context)
:ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override {
Finder->addMatcher(ast_matchers::callExpr().bind("foo"), this);
}
void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
if (!Aliaser)
Aliaser.reset(new NamespaceAliaser(*Result.SourceManager));
const auto *Call = Result.Nodes.getNodeAs<CallExpr>("foo");
assert(Call != nullptr && "Did not find node \"foo\"");
auto Hint = Aliaser->createAlias(*Result.Context, *Call, "::foo::bar",
{"b", "some_alias"});
if (Hint.hasValue())
diag(Call->getLocStart(), "Fix for testing") << Hint.getValue();
diag(Call->getLocStart(), "insert call")
<< FixItHint::CreateInsertion(
Call->getLocStart(),
Aliaser->getNamespaceName(*Result.Context, *Call, "::foo::bar") +
"::");
}
private:
std::unique_ptr<NamespaceAliaser> Aliaser;
};
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(NamespaceAliaserTest, AddNewAlias) {
EXPECT_EQ("#include \"foo.h\"\n"
"void f() {\n"
"namespace b = ::foo::bar;"
" b::f(); }",
runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
"void f() { f(); }",
2));
}
TEST(NamespaceAliaserTest, ReuseAlias) {
EXPECT_EQ(
"#include \"foo.h\"\n"
"void f() { namespace x = foo::bar; x::f(); }",
runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
"void f() { namespace x = foo::bar; f(); }",
1));
}
TEST(NamespaceAliaserTest, AddsOnlyOneAlias) {
EXPECT_EQ("#include \"foo.h\"\n"
"void f() {\n"
"namespace b = ::foo::bar;"
" b::f(); b::f(); }",
runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
"void f() { f(); f(); }",
3));
}
TEST(NamespaceAliaserTest, LocalConflict) {
EXPECT_EQ("#include \"foo.h\"\n"
"void f() {\n"
"namespace some_alias = ::foo::bar;"
" namespace b = foo; some_alias::f(); }",
runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
"void f() { namespace b = foo; f(); }",
2));
}
TEST(NamespaceAliaserTest, GlobalConflict) {
EXPECT_EQ("#include \"foo.h\"\n"
"namespace b = foo;\n"
"void f() {\n"
"namespace some_alias = ::foo::bar;"
" some_alias::f(); }",
runChecker<InsertAliasCheck>("#include \"foo.h\"\n"
"namespace b = foo;\n"
"void f() { f(); }",
2));
}
} // namespace utils
} // namespace tidy
} // namespace clang

View File

@@ -0,0 +1,49 @@
//===---- ObjCModuleTest.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 "ClangTidyTest.h"
#include "gtest/gtest.h"
#include "objc/ForbiddenSubclassingCheck.h"
using namespace clang::tidy::objc;
namespace clang {
namespace tidy {
namespace test {
TEST(ObjCForbiddenSubclassing, AllowedSubclass) {
std::vector<ClangTidyError> Errors;
runCheckOnCode<ForbiddenSubclassingCheck>(
"@interface Foo\n"
"@end\n"
"@interface Bar : Foo\n"
"@end\n",
&Errors,
"input.m");
EXPECT_EQ(0ul, Errors.size());
}
TEST(ObjCForbiddenSubclassing, ForbiddenSubclass) {
std::vector<ClangTidyError> Errors;
runCheckOnCode<ForbiddenSubclassingCheck>(
"@interface UIImagePickerController\n"
"@end\n"
"@interface Foo : UIImagePickerController\n"
"@end\n",
&Errors,
"input.m");
EXPECT_EQ(1ul, Errors.size());
EXPECT_EQ(
"Objective-C interface 'Foo' subclasses 'UIImagePickerController', which is not intended to be subclassed",
Errors[0].Message.Message);
}
} // namespace test
} // namespace tidy
} // namespace clang

Some files were not shown because too many files have changed in this diff Show More