summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
Diffstat (limited to 'unittests')
-rw-r--r--unittests/ASTMatchers/Dynamic/ParserTest.cpp28
-rw-r--r--unittests/ASTMatchers/Dynamic/RegistryTest.cpp43
-rw-r--r--unittests/ASTMatchers/Dynamic/VariantValueTest.cpp25
-rw-r--r--unittests/Basic/SourceManagerTest.cpp24
-rw-r--r--unittests/Format/FormatTestComments.cpp33
-rw-r--r--unittests/Format/FormatTestJS.cpp45
-rw-r--r--unittests/Frontend/FrontendActionTest.cpp5
-rw-r--r--unittests/Lex/LexerTest.cpp20
-rw-r--r--unittests/Lex/PPCallbacksTest.cpp23
-rw-r--r--unittests/Lex/PPConditionalDirectiveRecordTest.cpp20
-rw-r--r--unittests/Tooling/CommentHandlerTest.cpp3
-rw-r--r--unittests/Tooling/ToolingTest.cpp2
12 files changed, 183 insertions, 88 deletions
diff --git a/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/unittests/ASTMatchers/Dynamic/ParserTest.cpp
index d241f5ba2f86e..ed184a8c14977 100644
--- a/unittests/ASTMatchers/Dynamic/ParserTest.cpp
+++ b/unittests/ASTMatchers/Dynamic/ParserTest.cpp
@@ -75,6 +75,30 @@ public:
ExpectedMatchersTy ExpectedMatchers;
};
+TEST(ParserTest, ParseBoolean) {
+ MockSema Sema;
+ Sema.parse("true");
+ Sema.parse("false");
+ EXPECT_EQ(2U, Sema.Values.size());
+ EXPECT_EQ(true, Sema.Values[0].getBoolean());
+ EXPECT_EQ(false, Sema.Values[1].getBoolean());
+}
+
+TEST(ParserTest, ParseDouble) {
+ MockSema Sema;
+ Sema.parse("1.0");
+ Sema.parse("2.0f");
+ Sema.parse("34.56e-78");
+ Sema.parse("4.E+6");
+ Sema.parse("1");
+ EXPECT_EQ(5U, Sema.Values.size());
+ EXPECT_EQ(1.0, Sema.Values[0].getDouble());
+ EXPECT_EQ("1:1: Error parsing numeric literal: <2.0f>", Sema.Errors[1]);
+ EXPECT_EQ(34.56e-78, Sema.Values[2].getDouble());
+ EXPECT_EQ(4e+6, Sema.Values[3].getDouble());
+ EXPECT_FALSE(Sema.Values[4].isDouble());
+}
+
TEST(ParserTest, ParseUnsigned) {
MockSema Sema;
Sema.parse("0");
@@ -86,8 +110,8 @@ TEST(ParserTest, ParseUnsigned) {
EXPECT_EQ(0U, Sema.Values[0].getUnsigned());
EXPECT_EQ(123U, Sema.Values[1].getUnsigned());
EXPECT_EQ(31U, Sema.Values[2].getUnsigned());
- EXPECT_EQ("1:1: Error parsing unsigned token: <12345678901>", Sema.Errors[3]);
- EXPECT_EQ("1:1: Error parsing unsigned token: <1a1>", Sema.Errors[4]);
+ EXPECT_EQ("1:1: Error parsing numeric literal: <12345678901>", Sema.Errors[3]);
+ EXPECT_EQ("1:1: Error parsing numeric literal: <1a1>", Sema.Errors[4]);
}
TEST(ParserTest, ParseString) {
diff --git a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
index 6bbbc2bd3563a..aae229bd045a4 100644
--- a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
+++ b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp
@@ -511,6 +511,49 @@ TEST_F(RegistryTest, ParenExpr) {
EXPECT_FALSE(matches("int i = 1;", Value));
}
+TEST_F(RegistryTest, EqualsMatcher) {
+ Matcher<Stmt> BooleanStmt = constructMatcher(
+ "cxxBoolLiteral", constructMatcher("equals", VariantValue(true)))
+ .getTypedMatcher<Stmt>();
+ EXPECT_TRUE(matches("bool x = true;", BooleanStmt));
+ EXPECT_FALSE(matches("bool x = false;", BooleanStmt));
+ EXPECT_FALSE(matches("bool x = 0;", BooleanStmt));
+
+ BooleanStmt = constructMatcher(
+ "cxxBoolLiteral", constructMatcher("equals", VariantValue(0)))
+ .getTypedMatcher<Stmt>();
+ EXPECT_TRUE(matches("bool x = false;", BooleanStmt));
+ EXPECT_FALSE(matches("bool x = true;", BooleanStmt));
+ EXPECT_FALSE(matches("bool x = 0;", BooleanStmt));
+
+ Matcher<Stmt> DoubleStmt = constructMatcher(
+ "floatLiteral", constructMatcher("equals", VariantValue(1.2)))
+ .getTypedMatcher<Stmt>();
+ EXPECT_TRUE(matches("double x = 1.2;", DoubleStmt));
+#if 0
+ // FIXME floatLiteral matching should work regardless of suffix.
+ EXPECT_TRUE(matches("double x = 1.2f;", DoubleStmt));
+ EXPECT_TRUE(matches("double x = 1.2l;", DoubleStmt));
+#endif
+ EXPECT_TRUE(matches("double x = 12e-1;", DoubleStmt));
+ EXPECT_FALSE(matches("double x = 1.23;", DoubleStmt));
+
+ Matcher<Stmt> IntegerStmt = constructMatcher(
+ "integerLiteral", constructMatcher("equals", VariantValue(42)))
+ .getTypedMatcher<Stmt>();
+ EXPECT_TRUE(matches("int x = 42;", IntegerStmt));
+ EXPECT_FALSE(matches("int x = 1;", IntegerStmt));
+
+ Matcher<Stmt> CharStmt = constructMatcher(
+ "characterLiteral", constructMatcher("equals", VariantValue('x')))
+ .getTypedMatcher<Stmt>();
+ EXPECT_TRUE(matches("int x = 'x';", CharStmt));
+ EXPECT_TRUE(matches("int x = L'x';", CharStmt));
+ EXPECT_TRUE(matches("int x = u'x';", CharStmt));
+ EXPECT_TRUE(matches("int x = U'x';", CharStmt));
+ EXPECT_FALSE(matches("int x = 120;", CharStmt));
+}
+
} // end anonymous namespace
} // end namespace dynamic
} // end namespace ast_matchers
diff --git a/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp b/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
index 9df7b780d47f6..7d3a07028a1ba 100644
--- a/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
+++ b/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp
@@ -75,12 +75,16 @@ TEST(VariantValueTest, Assignment) {
EXPECT_TRUE(Value.isString());
EXPECT_EQ("A", Value.getString());
EXPECT_TRUE(Value.hasValue());
+ EXPECT_FALSE(Value.isBoolean());
+ EXPECT_FALSE(Value.isDouble());
EXPECT_FALSE(Value.isUnsigned());
EXPECT_FALSE(Value.isMatcher());
EXPECT_EQ("String", Value.getTypeAsString());
Value = VariantMatcher::SingleMatcher(recordDecl());
EXPECT_TRUE(Value.hasValue());
+ EXPECT_FALSE(Value.isBoolean());
+ EXPECT_FALSE(Value.isDouble());
EXPECT_FALSE(Value.isUnsigned());
EXPECT_FALSE(Value.isString());
EXPECT_TRUE(Value.isMatcher());
@@ -88,15 +92,36 @@ TEST(VariantValueTest, Assignment) {
EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
+ Value = true;
+ EXPECT_TRUE(Value.isBoolean());
+ EXPECT_EQ(true, Value.getBoolean());
+ EXPECT_TRUE(Value.hasValue());
+ EXPECT_FALSE(Value.isUnsigned());
+ EXPECT_FALSE(Value.isMatcher());
+ EXPECT_FALSE(Value.isString());
+
+ Value = 3.14;
+ EXPECT_TRUE(Value.isDouble());
+ EXPECT_EQ(3.14, Value.getDouble());
+ EXPECT_TRUE(Value.hasValue());
+ EXPECT_FALSE(Value.isBoolean());
+ EXPECT_FALSE(Value.isUnsigned());
+ EXPECT_FALSE(Value.isMatcher());
+ EXPECT_FALSE(Value.isString());
+
Value = 17;
EXPECT_TRUE(Value.isUnsigned());
EXPECT_EQ(17U, Value.getUnsigned());
+ EXPECT_FALSE(Value.isBoolean());
+ EXPECT_FALSE(Value.isDouble());
EXPECT_TRUE(Value.hasValue());
EXPECT_FALSE(Value.isMatcher());
EXPECT_FALSE(Value.isString());
Value = VariantValue();
EXPECT_FALSE(Value.hasValue());
+ EXPECT_FALSE(Value.isBoolean());
+ EXPECT_FALSE(Value.isDouble());
EXPECT_FALSE(Value.isUnsigned());
EXPECT_FALSE(Value.isString());
EXPECT_FALSE(Value.isMatcher());
diff --git a/unittests/Basic/SourceManagerTest.cpp b/unittests/Basic/SourceManagerTest.cpp
index aa15e16b851f4..8457d3b639f2c 100644
--- a/unittests/Basic/SourceManagerTest.cpp
+++ b/unittests/Basic/SourceManagerTest.cpp
@@ -51,24 +51,6 @@ protected:
IntrusiveRefCntPtr<TargetInfo> Target;
};
-class VoidModuleLoader : public ModuleLoader {
- ModuleLoadResult loadModule(SourceLocation ImportLoc,
- ModuleIdPath Path,
- Module::NameVisibilityKind Visibility,
- bool IsInclusionDirective) override {
- return ModuleLoadResult();
- }
-
- void makeModuleVisible(Module *Mod,
- Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc) override { }
-
- GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
- { return nullptr; }
- bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
- { return 0; }
-};
-
TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
const char *source =
"#define M(x) [x]\n"
@@ -78,7 +60,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
SourceMgr.setMainFileID(mainFileID);
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, LangOpts, &*Target);
@@ -199,7 +181,7 @@ TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
HeaderBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, LangOpts, &*Target);
@@ -318,7 +300,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
HeaderBuf->getBufferSize(), 0);
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, LangOpts, &*Target);
diff --git a/unittests/Format/FormatTestComments.cpp b/unittests/Format/FormatTestComments.cpp
index a44a6845587da..fdb5a08e7a219 100644
--- a/unittests/Format/FormatTestComments.cpp
+++ b/unittests/Format/FormatTestComments.cpp
@@ -1052,6 +1052,30 @@ TEST_F(FormatTestComments, KeepsTrailingPPCommentsAndSectionCommentsSeparate) {
"}", getLLVMStyleWithColumns(80));
}
+TEST_F(FormatTestComments, AlignsPPElseEndifComments) {
+ verifyFormat("#if A\n"
+ "#else // A\n"
+ "int iiii;\n"
+ "#endif // B",
+ getLLVMStyleWithColumns(20));
+ verifyFormat("#if A\n"
+ "#else // A\n"
+ "int iiii; // CC\n"
+ "#endif // B",
+ getLLVMStyleWithColumns(20));
+ EXPECT_EQ("#if A\n"
+ "#else // A1\n"
+ " // A2\n"
+ "int ii;\n"
+ "#endif // B",
+ format("#if A\n"
+ "#else // A1\n"
+ " // A2\n"
+ "int ii;\n"
+ "#endif // B",
+ getLLVMStyleWithColumns(20)));
+}
+
TEST_F(FormatTestComments, CommentsInStaticInitializers) {
EXPECT_EQ(
"static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
@@ -2170,6 +2194,15 @@ TEST_F(FormatTestComments, AlignTrailingComments) {
"// long",
getLLVMStyleWithColumns(15)));
+ // Don't align newly broken trailing comments if that would put them over the
+ // column limit.
+ EXPECT_EQ("int i, j; // line 1\n"
+ "int k; // line longg\n"
+ " // long",
+ format("int i, j; // line 1\n"
+ "int k; // line longg long",
+ getLLVMStyleWithColumns(20)));
+
// Align comment line sections aligned with the next token with the next
// token.
EXPECT_EQ("class A {\n"
diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp
index 92a113111b6a7..e84f470687ec6 100644
--- a/unittests/Format/FormatTestJS.cpp
+++ b/unittests/Format/FormatTestJS.cpp
@@ -1226,6 +1226,12 @@ TEST_F(FormatTestJS, UnionIntersectionTypes) {
verifyFormat("let x: Bar|Baz;");
verifyFormat("let x: Bar<X>|Baz;");
verifyFormat("let x: (Foo|Bar)[];");
+ verifyFormat("type X = {\n"
+ " a: Foo|Bar;\n"
+ "};");
+ verifyFormat("export type X = {\n"
+ " a: Foo|Bar;\n"
+ "};");
}
TEST_F(FormatTestJS, ClassDeclarations) {
@@ -1869,5 +1875,44 @@ TEST_F(FormatTestJS, Exponentiation) {
verifyFormat("squared **= 2;");
}
+TEST_F(FormatTestJS, NestedLiterals) {
+ FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
+ FourSpaces.IndentWidth = 4;
+ verifyFormat("var l = [\n"
+ " [\n"
+ " 1,\n"
+ " ],\n"
+ "];", FourSpaces);
+ verifyFormat("var l = [\n"
+ " {\n"
+ " 1: 1,\n"
+ " },\n"
+ "];", FourSpaces);
+ verifyFormat("someFunction(\n"
+ " p1,\n"
+ " [\n"
+ " 1,\n"
+ " ],\n"
+ ");", FourSpaces);
+ verifyFormat("someFunction(\n"
+ " p1,\n"
+ " {\n"
+ " 1: 1,\n"
+ " },\n"
+ ");", FourSpaces);
+ verifyFormat("var o = {\n"
+ " 1: 1,\n"
+ " 2: {\n"
+ " 3: 3,\n"
+ " },\n"
+ "};", FourSpaces);
+ verifyFormat("var o = {\n"
+ " 1: 1,\n"
+ " 2: [\n"
+ " 3,\n"
+ " ],\n"
+ "};", FourSpaces);
+}
+
} // end namespace tooling
} // end namespace clang
diff --git a/unittests/Frontend/FrontendActionTest.cpp b/unittests/Frontend/FrontendActionTest.cpp
index 68181c705ce8c..ce0144538db7f 100644
--- a/unittests/Frontend/FrontendActionTest.cpp
+++ b/unittests/Frontend/FrontendActionTest.cpp
@@ -37,12 +37,11 @@ public:
bool ActOnEndOfTranslationUnit;
std::vector<std::string> decl_names;
- bool BeginSourceFileAction(CompilerInstance &ci,
- StringRef filename) override {
+ bool BeginSourceFileAction(CompilerInstance &ci) override {
if (EnableIncrementalProcessing)
ci.getPreprocessor().enableIncrementalProcessing();
- return ASTFrontendAction::BeginSourceFileAction(ci, filename);
+ return ASTFrontendAction::BeginSourceFileAction(ci);
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
diff --git a/unittests/Lex/LexerTest.cpp b/unittests/Lex/LexerTest.cpp
index b5a6fd90d08fd..e2507d3580d37 100644
--- a/unittests/Lex/LexerTest.cpp
+++ b/unittests/Lex/LexerTest.cpp
@@ -27,24 +27,6 @@ using namespace clang;
namespace {
-class VoidModuleLoader : public ModuleLoader {
- ModuleLoadResult loadModule(SourceLocation ImportLoc,
- ModuleIdPath Path,
- Module::NameVisibilityKind Visibility,
- bool IsInclusionDirective) override {
- return ModuleLoadResult();
- }
-
- void makeModuleVisible(Module *Mod,
- Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc) override { }
-
- GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
- { return nullptr; }
- bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
- { return 0; }
-};
-
// The test fixture.
class LexerTest : public ::testing::Test {
protected:
@@ -64,7 +46,7 @@ protected:
llvm::MemoryBuffer::getMemBuffer(Source);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, LangOpts, Target.get());
diff --git a/unittests/Lex/PPCallbacksTest.cpp b/unittests/Lex/PPCallbacksTest.cpp
index 0841791e08638..67b56a601c71a 100644
--- a/unittests/Lex/PPCallbacksTest.cpp
+++ b/unittests/Lex/PPCallbacksTest.cpp
@@ -32,25 +32,6 @@ using namespace clang;
namespace {
-// Stub out module loading.
-class VoidModuleLoader : public ModuleLoader {
- ModuleLoadResult loadModule(SourceLocation ImportLoc,
- ModuleIdPath Path,
- Module::NameVisibilityKind Visibility,
- bool IsInclusionDirective) override {
- return ModuleLoadResult();
- }
-
- void makeModuleVisible(Module *Mod,
- Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc) override { }
-
- GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
- { return nullptr; }
- bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
- { return 0; }
-};
-
// Stub to collect data from InclusionDirective callbacks.
class InclusionDirectiveCallbacks : public PPCallbacks {
public:
@@ -161,7 +142,7 @@ protected:
llvm::MemoryBuffer::getMemBuffer(SourceText);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
@@ -199,7 +180,7 @@ protected:
llvm::MemoryBuffer::getMemBuffer(SourceText, "test.cl");
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, OpenCLLangOpts, Target.get());
diff --git a/unittests/Lex/PPConditionalDirectiveRecordTest.cpp b/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
index b635604e0982c..f7b6f717a1db9 100644
--- a/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
+++ b/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
@@ -51,24 +51,6 @@ protected:
IntrusiveRefCntPtr<TargetInfo> Target;
};
-class VoidModuleLoader : public ModuleLoader {
- ModuleLoadResult loadModule(SourceLocation ImportLoc,
- ModuleIdPath Path,
- Module::NameVisibilityKind Visibility,
- bool IsInclusionDirective) override {
- return ModuleLoadResult();
- }
-
- void makeModuleVisible(Module *Mod,
- Module::NameVisibilityKind Visibility,
- SourceLocation ImportLoc) override { }
-
- GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
- { return nullptr; }
- bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
- { return 0; }
-};
-
TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
const char *source =
"0 1\n"
@@ -93,7 +75,7 @@ TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
llvm::MemoryBuffer::getMemBuffer(source);
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
- VoidModuleLoader ModLoader;
+ TrivialModuleLoader ModLoader;
MemoryBufferCache PCMCache;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, LangOpts, Target.get());
diff --git a/unittests/Tooling/CommentHandlerTest.cpp b/unittests/Tooling/CommentHandlerTest.cpp
index b42b28b9c9486..9c3abdc4b15e6 100644
--- a/unittests/Tooling/CommentHandlerTest.cpp
+++ b/unittests/Tooling/CommentHandlerTest.cpp
@@ -70,8 +70,7 @@ private:
CommentHandlerAction(CommentHandlerVisitor *Visitor)
: TestAction(Visitor) { }
- bool BeginSourceFileAction(CompilerInstance &CI,
- StringRef FileName) override {
+ bool BeginSourceFileAction(CompilerInstance &CI) override {
CommentHandlerVisitor *V =
static_cast<CommentHandlerVisitor*>(this->Visitor);
V->PP = &CI.getPreprocessor();
diff --git a/unittests/Tooling/ToolingTest.cpp b/unittests/Tooling/ToolingTest.cpp
index 68b5ed21059bc..b179f033d3579 100644
--- a/unittests/Tooling/ToolingTest.cpp
+++ b/unittests/Tooling/ToolingTest.cpp
@@ -203,7 +203,7 @@ TEST(ToolInvocation, TestVirtualModulesCompilation) {
struct VerifyEndCallback : public SourceFileCallbacks {
VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
- bool handleBeginSource(CompilerInstance &CI, StringRef Filename) override {
+ bool handleBeginSource(CompilerInstance &CI) override {
++BeginCalled;
return true;
}