summaryrefslogtreecommitdiffstats
path: root/unittests/AST/SourceLocationTest.cpp
diff options
context:
space:
mode:
authordim <dim@FreeBSD.org>2013-04-08 18:45:10 +0000
committerdim <dim@FreeBSD.org>2013-04-08 18:45:10 +0000
commitc72c57c9e9b69944e3e009cd5e209634839581d3 (patch)
tree4fc2f184c499d106f29a386c452b49e5197bf63d /unittests/AST/SourceLocationTest.cpp
parent5b20025c30d23d521e12c1f33ec8fa6b821952cd (diff)
downloadFreeBSD-src-c72c57c9e9b69944e3e009cd5e209634839581d3.zip
FreeBSD-src-c72c57c9e9b69944e3e009cd5e209634839581d3.tar.gz
Vendor import of clang trunk r178860:
http://llvm.org/svn/llvm-project/cfe/trunk@178860
Diffstat (limited to 'unittests/AST/SourceLocationTest.cpp')
-rw-r--r--unittests/AST/SourceLocationTest.cpp202
1 files changed, 36 insertions, 166 deletions
diff --git a/unittests/AST/SourceLocationTest.cpp b/unittests/AST/SourceLocationTest.cpp
index dec833d..b8d8b02 100644
--- a/unittests/AST/SourceLocationTest.cpp
+++ b/unittests/AST/SourceLocationTest.cpp
@@ -17,179 +17,16 @@
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTContext.h"
-#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"
+#include "MatchVerifier.h"
namespace clang {
namespace ast_matchers {
-using clang::tooling::newFrontendActionFactory;
-using clang::tooling::runToolOnCodeWithArgs;
-using clang::tooling::FrontendActionFactory;
-
-enum Language { Lang_C, Lang_C89, Lang_CXX };
-
-/// \brief Base class for verifying some property of nodes found by a matcher.
-///
-/// FIXME: This class should be shared with other AST tests.
-template <typename NodeType>
-class MatchVerifier : public MatchFinder::MatchCallback {
-public:
- template <typename MatcherType>
- testing::AssertionResult match(const std::string &Code,
- const MatcherType &AMatcher) {
- return match(Code, AMatcher, Lang_CXX);
- }
-
- template <typename MatcherType>
- testing::AssertionResult match(const std::string &Code,
- const MatcherType &AMatcher, Language L);
-
-protected:
- virtual void run(const MatchFinder::MatchResult &Result);
- virtual void verify(const MatchFinder::MatchResult &Result,
- const NodeType &Node) = 0;
-
- void setFailure(const Twine &Result) {
- Verified = false;
- VerifyResult = Result.str();
- }
-
-private:
- bool Verified;
- std::string VerifyResult;
-};
-
-/// \brief Runs a matcher over some code, and returns the result of the
-/// verifier for the matched node.
-template <typename NodeType> template <typename MatcherType>
-testing::AssertionResult MatchVerifier<NodeType>::match(
- const std::string &Code, const MatcherType &AMatcher, Language L) {
- MatchFinder Finder;
- Finder.addMatcher(AMatcher.bind(""), this);
- OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
-
- std::vector<std::string> Args;
- StringRef FileName;
- switch (L) {
- case Lang_C:
- Args.push_back("-std=c99");
- FileName = "input.c";
- break;
- case Lang_C89:
- Args.push_back("-std=c89");
- FileName = "input.c";
- break;
- case Lang_CXX:
- Args.push_back("-std=c++98");
- FileName = "input.cc";
- break;
- }
-
- // Default to failure in case callback is never called
- setFailure("Could not find match");
- if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, FileName))
- return testing::AssertionFailure() << "Parsing error";
- if (!Verified)
- return testing::AssertionFailure() << VerifyResult;
- return testing::AssertionSuccess();
-}
-
-template <typename NodeType>
-void MatchVerifier<NodeType>::run(const MatchFinder::MatchResult &Result) {
- const NodeType *Node = Result.Nodes.getNodeAs<NodeType>("");
- if (!Node) {
- setFailure("Matched node has wrong type");
- } else {
- // Callback has been called, default to success
- Verified = true;
- verify(Result, *Node);
- }
-}
-
-/// \brief Verify whether a node has the correct source location.
-///
-/// By default, Node.getSourceLocation() is checked. This can be changed
-/// by overriding getLocation().
-template <typename NodeType>
-class LocationVerifier : public MatchVerifier<NodeType> {
-public:
- void expectLocation(unsigned Line, unsigned Column) {
- ExpectLine = Line;
- ExpectColumn = Column;
- }
-
-protected:
- void verify(const MatchFinder::MatchResult &Result, const NodeType &Node) {
- SourceLocation Loc = getLocation(Node);
- unsigned Line = Result.SourceManager->getSpellingLineNumber(Loc);
- unsigned Column = Result.SourceManager->getSpellingColumnNumber(Loc);
- if (Line != ExpectLine || Column != ExpectColumn) {
- std::string MsgStr;
- llvm::raw_string_ostream Msg(MsgStr);
- Msg << "Expected location <" << ExpectLine << ":" << ExpectColumn
- << ">, found <";
- Loc.print(Msg, *Result.SourceManager);
- Msg << '>';
- this->setFailure(Msg.str());
- }
- }
-
- virtual SourceLocation getLocation(const NodeType &Node) {
- return Node.getLocation();
- }
-
-private:
- unsigned ExpectLine, ExpectColumn;
-};
-
-/// \brief Verify whether a node has the correct source range.
-///
-/// By default, Node.getSourceRange() is checked. This can be changed
-/// by overriding getRange().
-template <typename NodeType>
-class RangeVerifier : public MatchVerifier<NodeType> {
-public:
- void expectRange(unsigned BeginLine, unsigned BeginColumn,
- unsigned EndLine, unsigned EndColumn) {
- ExpectBeginLine = BeginLine;
- ExpectBeginColumn = BeginColumn;
- ExpectEndLine = EndLine;
- ExpectEndColumn = EndColumn;
- }
-
-protected:
- void verify(const MatchFinder::MatchResult &Result, const NodeType &Node) {
- SourceRange R = getRange(Node);
- SourceLocation Begin = R.getBegin();
- SourceLocation End = R.getEnd();
- unsigned BeginLine = Result.SourceManager->getSpellingLineNumber(Begin);
- unsigned BeginColumn = Result.SourceManager->getSpellingColumnNumber(Begin);
- unsigned EndLine = Result.SourceManager->getSpellingLineNumber(End);
- unsigned EndColumn = Result.SourceManager->getSpellingColumnNumber(End);
- if (BeginLine != ExpectBeginLine || BeginColumn != ExpectBeginColumn ||
- EndLine != ExpectEndLine || EndColumn != ExpectEndColumn) {
- std::string MsgStr;
- llvm::raw_string_ostream Msg(MsgStr);
- Msg << "Expected range <" << ExpectBeginLine << ":" << ExpectBeginColumn
- << '-' << ExpectEndLine << ":" << ExpectEndColumn << ">, found <";
- Begin.print(Msg, *Result.SourceManager);
- Msg << '-';
- End.print(Msg, *Result.SourceManager);
- Msg << '>';
- this->setFailure(Msg.str());
- }
- }
-
- virtual SourceRange getRange(const NodeType &Node) {
- return Node.getSourceRange();
- }
-
-private:
- unsigned ExpectBeginLine, ExpectBeginColumn, ExpectEndLine, ExpectEndColumn;
-};
+// FIXME: Pull the *Verifier tests into their own test file.
TEST(MatchVerifier, ParseError) {
LocationVerifier<VarDecl> Verifier;
@@ -285,5 +122,38 @@ TEST(CXXConstructorDecl, NoRetFunTypeLocRange) {
EXPECT_TRUE(Verifier.match("class C { C(); };", functionDecl()));
}
+TEST(CompoundLiteralExpr, CompoundVectorLiteralRange) {
+ RangeVerifier<CompoundLiteralExpr> Verifier;
+ Verifier.expectRange(2, 11, 2, 22);
+ EXPECT_TRUE(Verifier.match(
+ "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+ "int2 i2 = (int2){1, 2};", compoundLiteralExpr()));
+}
+
+TEST(CompoundLiteralExpr, ParensCompoundVectorLiteralRange) {
+ RangeVerifier<CompoundLiteralExpr> Verifier;
+ Verifier.expectRange(2, 11, 2, 22);
+ EXPECT_TRUE(Verifier.match(
+ "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+ "int2 i2 = (int2)(1, 2);",
+ compoundLiteralExpr(), Lang_OpenCL));
+}
+
+TEST(InitListExpr, VectorLiteralListBraceRange) {
+ RangeVerifier<InitListExpr> Verifier;
+ Verifier.expectRange(2, 17, 2, 22);
+ EXPECT_TRUE(Verifier.match(
+ "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+ "int2 i2 = (int2){1, 2};", initListExpr()));
+}
+
+TEST(InitListExpr, VectorLiteralInitListParens) {
+ RangeVerifier<InitListExpr> Verifier;
+ Verifier.expectRange(2, 17, 2, 22);
+ EXPECT_TRUE(Verifier.match(
+ "typedef int int2 __attribute__((ext_vector_type(2)));\n"
+ "int2 i2 = (int2)(1, 2);", initListExpr(), Lang_OpenCL));
+}
+
} // end namespace ast_matchers
} // end namespace clang
OpenPOWER on IntegriCloud