2012-06-25 07:56:26 +08:00
|
|
|
//===--- TestVisitor.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-08-24 14:59:51 +08:00
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Defines utility templates for RecursiveASTVisitor related tests.
|
|
|
|
///
|
2012-06-25 07:56:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TEST_VISITOR_H
|
|
|
|
#define LLVM_CLANG_TEST_VISITOR_H
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2012-06-25 07:56:26 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2012-06-25 07:56:26 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "gtest/gtest.h"
|
2012-12-04 17:45:34 +08:00
|
|
|
#include <vector>
|
2012-06-25 07:56:26 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
2012-06-26 02:27:11 +08:00
|
|
|
/// \brief Base class for simple RecursiveASTVisitor based tests.
|
2012-06-25 07:56:26 +08:00
|
|
|
///
|
|
|
|
/// This is a drop-in replacement for RecursiveASTVisitor itself, with the
|
|
|
|
/// additional capability of running it over a snippet of code.
|
|
|
|
///
|
2013-09-11 23:53:29 +08:00
|
|
|
/// Visits template instantiations and implicit code by default.
|
2012-06-25 07:56:26 +08:00
|
|
|
template <typename T>
|
|
|
|
class TestVisitor : public RecursiveASTVisitor<T> {
|
|
|
|
public:
|
|
|
|
TestVisitor() { }
|
|
|
|
|
2012-06-26 02:27:11 +08:00
|
|
|
virtual ~TestVisitor() { }
|
|
|
|
|
2014-06-26 09:42:24 +08:00
|
|
|
enum Language {
|
|
|
|
Lang_C,
|
|
|
|
Lang_CXX98,
|
|
|
|
Lang_CXX11,
|
|
|
|
Lang_OBJC,
|
|
|
|
Lang_OBJCXX11,
|
|
|
|
Lang_CXX = Lang_CXX98
|
|
|
|
};
|
2012-08-18 05:23:17 +08:00
|
|
|
|
2012-06-25 07:56:26 +08:00
|
|
|
/// \brief Runs the current AST visitor over the given code.
|
2012-08-18 05:23:17 +08:00
|
|
|
bool runOver(StringRef Code, Language L = Lang_CXX) {
|
2012-08-30 10:02:19 +08:00
|
|
|
std::vector<std::string> Args;
|
|
|
|
switch (L) {
|
|
|
|
case Lang_C: Args.push_back("-std=c99"); break;
|
2013-06-30 11:05:49 +08:00
|
|
|
case Lang_CXX98: Args.push_back("-std=c++98"); break;
|
|
|
|
case Lang_CXX11: Args.push_back("-std=c++11"); break;
|
2014-06-06 23:05:09 +08:00
|
|
|
case Lang_OBJC: Args.push_back("-ObjC"); break;
|
2014-06-26 09:42:24 +08:00
|
|
|
case Lang_OBJCXX11:
|
|
|
|
Args.push_back("-ObjC++");
|
|
|
|
Args.push_back("-std=c++11");
|
2014-06-26 10:07:06 +08:00
|
|
|
Args.push_back("-fblocks");
|
2014-06-26 09:42:24 +08:00
|
|
|
break;
|
2012-08-30 10:02:19 +08:00
|
|
|
}
|
|
|
|
return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args);
|
2012-06-25 07:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldVisitTemplateInstantiations() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 23:53:29 +08:00
|
|
|
bool shouldVisitImplicitCode() const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-25 07:56:26 +08:00
|
|
|
protected:
|
|
|
|
virtual ASTFrontendAction* CreateTestAction() {
|
|
|
|
return new TestAction(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
class FindConsumer : public ASTConsumer {
|
|
|
|
public:
|
|
|
|
FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
|
|
|
|
|
|
|
|
virtual void HandleTranslationUnit(clang::ASTContext &Context) {
|
2012-07-06 02:13:01 +08:00
|
|
|
Visitor->Context = &Context;
|
2012-06-25 07:56:26 +08:00
|
|
|
Visitor->TraverseDecl(Context.getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TestVisitor *Visitor;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestAction : public ASTFrontendAction {
|
|
|
|
public:
|
|
|
|
TestAction(TestVisitor *Visitor) : Visitor(Visitor) {}
|
|
|
|
|
2014-07-18 06:34:12 +08:00
|
|
|
virtual clang::ASTConsumer* CreateASTConsumer(
|
|
|
|
CompilerInstance&, llvm::StringRef dummy) {
|
2012-06-25 07:56:26 +08:00
|
|
|
/// TestConsumer will be deleted by the framework calling us.
|
2014-07-18 06:34:12 +08:00
|
|
|
return new FindConsumer(Visitor);
|
2012-06-25 07:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
TestVisitor *Visitor;
|
|
|
|
};
|
|
|
|
|
|
|
|
ASTContext *Context;
|
|
|
|
};
|
|
|
|
|
2012-08-24 14:59:51 +08:00
|
|
|
/// \brief A RecursiveASTVisitor to check that certain matches are (or are
|
|
|
|
/// not) observed during visitation.
|
2012-06-25 07:56:26 +08:00
|
|
|
///
|
2012-08-24 14:59:51 +08:00
|
|
|
/// This is a RecursiveASTVisitor for testing the RecursiveASTVisitor itself,
|
|
|
|
/// and allows simple creation of test visitors running matches on only a small
|
2012-06-25 07:56:26 +08:00
|
|
|
/// subset of the Visit* methods.
|
|
|
|
template <typename T, template <typename> class Visitor = TestVisitor>
|
|
|
|
class ExpectedLocationVisitor : public Visitor<T> {
|
|
|
|
public:
|
2012-08-24 14:59:51 +08:00
|
|
|
/// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
|
|
|
|
///
|
|
|
|
/// Any number of matches can be disallowed.
|
|
|
|
void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
|
|
|
|
DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
|
2012-06-25 07:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Expect 'Match' to occur at the given 'Line' and 'Column'.
|
2012-08-24 14:59:51 +08:00
|
|
|
///
|
|
|
|
/// Any number of expected matches can be set by calling this repeatedly.
|
|
|
|
/// Each is expected to be matched exactly once.
|
2012-06-25 07:56:26 +08:00
|
|
|
void ExpectMatch(Twine Match, unsigned Line, unsigned Column) {
|
2012-08-24 14:59:51 +08:00
|
|
|
ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Checks that all expected matches have been found.
|
|
|
|
virtual ~ExpectedLocationVisitor() {
|
|
|
|
for (typename std::vector<ExpectedMatch>::const_iterator
|
|
|
|
It = ExpectedMatches.begin(), End = ExpectedMatches.end();
|
|
|
|
It != End; ++It) {
|
|
|
|
It->ExpectFound();
|
|
|
|
}
|
2012-06-25 07:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-08-24 14:59:51 +08:00
|
|
|
/// \brief Checks an actual match against expected and disallowed matches.
|
2012-06-25 07:56:26 +08:00
|
|
|
///
|
|
|
|
/// Implementations are required to call this with appropriate values
|
|
|
|
/// for 'Name' during visitation.
|
|
|
|
void Match(StringRef Name, SourceLocation Location) {
|
2012-08-24 14:59:51 +08:00
|
|
|
const FullSourceLoc FullLocation = this->Context->getFullLoc(Location);
|
|
|
|
|
|
|
|
for (typename std::vector<MatchCandidate>::const_iterator
|
|
|
|
It = DisallowedMatches.begin(), End = DisallowedMatches.end();
|
|
|
|
It != End; ++It) {
|
|
|
|
EXPECT_FALSE(It->Matches(Name, FullLocation))
|
|
|
|
<< "Matched disallowed " << *It;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (typename std::vector<ExpectedMatch>::iterator
|
|
|
|
It = ExpectedMatches.begin(), End = ExpectedMatches.end();
|
|
|
|
It != End; ++It) {
|
|
|
|
It->UpdateFor(Name, FullLocation, this->Context->getSourceManager());
|
2012-06-25 07:56:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 14:59:51 +08:00
|
|
|
private:
|
|
|
|
struct MatchCandidate {
|
|
|
|
std::string ExpectedName;
|
|
|
|
unsigned LineNumber;
|
|
|
|
unsigned ColumnNumber;
|
|
|
|
|
|
|
|
MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
|
|
|
|
: ExpectedName(Name.str()), LineNumber(LineNumber),
|
|
|
|
ColumnNumber(ColumnNumber) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Matches(StringRef Name, FullSourceLoc const &Location) const {
|
|
|
|
return MatchesName(Name) && MatchesLocation(Location);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartiallyMatches(StringRef Name, FullSourceLoc const &Location) const {
|
|
|
|
return MatchesName(Name) || MatchesLocation(Location);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchesName(StringRef Name) const {
|
|
|
|
return Name == ExpectedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchesLocation(FullSourceLoc const &Location) const {
|
|
|
|
return Location.isValid() &&
|
|
|
|
Location.getSpellingLineNumber() == LineNumber &&
|
|
|
|
Location.getSpellingColumnNumber() == ColumnNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend std::ostream &operator<<(std::ostream &Stream,
|
|
|
|
MatchCandidate const &Match) {
|
|
|
|
return Stream << Match.ExpectedName
|
|
|
|
<< " at " << Match.LineNumber << ":" << Match.ColumnNumber;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ExpectedMatch {
|
|
|
|
ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
|
|
|
|
: Candidate(Name, LineNumber, ColumnNumber), Found(false) {}
|
|
|
|
|
|
|
|
void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
|
|
|
|
if (Candidate.Matches(Name, Location)) {
|
|
|
|
EXPECT_TRUE(!Found);
|
|
|
|
Found = true;
|
|
|
|
} else if (!Found && Candidate.PartiallyMatches(Name, Location)) {
|
|
|
|
llvm::raw_string_ostream Stream(PartialMatches);
|
|
|
|
Stream << ", partial match: \"" << Name << "\" at ";
|
|
|
|
Location.print(Stream, SM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectFound() const {
|
|
|
|
EXPECT_TRUE(Found)
|
|
|
|
<< "Expected \"" << Candidate.ExpectedName
|
|
|
|
<< "\" at " << Candidate.LineNumber
|
|
|
|
<< ":" << Candidate.ColumnNumber << PartialMatches;
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchCandidate Candidate;
|
|
|
|
std::string PartialMatches;
|
|
|
|
bool Found;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<MatchCandidate> DisallowedMatches;
|
|
|
|
std::vector<ExpectedMatch> ExpectedMatches;
|
2012-06-25 07:56:26 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LLVM_CLANG_TEST_VISITOR_H */
|