2012-06-25 07:56:26 +08:00
|
|
|
//===--- TestVisitor.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-06-25 07:56:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H
|
|
|
|
#define LLVM_CLANG_UNITTESTS_TOOLING_TESTVISITOR_H
|
2012-06-25 07:56:26 +08:00
|
|
|
|
|
|
|
#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:
|
2015-10-20 21:23:58 +08:00
|
|
|
TestVisitor() { }
|
2012-06-25 07:56:26 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
virtual ~TestVisitor() { }
|
2012-06-26 02:27:11 +08:00
|
|
|
|
2014-06-26 09:42:24 +08:00
|
|
|
enum Language {
|
|
|
|
Lang_C,
|
|
|
|
Lang_CXX98,
|
|
|
|
Lang_CXX11,
|
2016-08-17 22:59:53 +08:00
|
|
|
Lang_CXX14,
|
2018-12-07 23:46:29 +08:00
|
|
|
Lang_CXX17,
|
|
|
|
Lang_CXX2a,
|
2014-06-26 09:42:24 +08:00
|
|
|
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) {
|
2017-06-27 15:59:56 +08:00
|
|
|
case Lang_C:
|
|
|
|
Args.push_back("-x");
|
|
|
|
Args.push_back("c");
|
|
|
|
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;
|
2016-08-17 22:59:53 +08:00
|
|
|
case Lang_CXX14: Args.push_back("-std=c++14"); break;
|
2018-12-07 23:46:29 +08:00
|
|
|
case Lang_CXX17: Args.push_back("-std=c++17"); break;
|
|
|
|
case Lang_CXX2a: Args.push_back("-std=c++2a"); break;
|
2017-08-30 23:28:01 +08:00
|
|
|
case Lang_OBJC:
|
|
|
|
Args.push_back("-ObjC");
|
|
|
|
Args.push_back("-fobjc-runtime=macosx-10.12.0");
|
|
|
|
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:
|
2018-02-27 23:54:55 +08:00
|
|
|
virtual ASTFrontendAction* CreateTestAction() {
|
|
|
|
return new TestAction(this);
|
2012-06-25 07:56:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class FindConsumer : public ASTConsumer {
|
|
|
|
public:
|
|
|
|
FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
void HandleTranslationUnit(clang::ASTContext &Context) override {
|
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) {}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
|
|
|
CreateASTConsumer(CompilerInstance &, llvm::StringRef dummy) override {
|
2012-06-25 07:56:26 +08:00
|
|
|
/// TestConsumer will be deleted by the framework calling us.
|
2014-08-11 03:56:51 +08:00
|
|
|
return llvm::make_unique<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.
|
2017-10-12 07:54:34 +08:00
|
|
|
void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
|
2012-08-24 14:59:51 +08:00
|
|
|
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.
|
2016-08-17 22:59:53 +08:00
|
|
|
/// Each is expected to be matched 'Times' number of times. (This is useful in
|
|
|
|
/// cases in which different AST nodes can match at the same source code
|
|
|
|
/// location.)
|
2017-10-12 07:54:34 +08:00
|
|
|
void ExpectMatch(Twine Match, unsigned Line, unsigned Column,
|
2016-08-17 22:59:53 +08:00
|
|
|
unsigned Times = 1) {
|
|
|
|
ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column, Times));
|
2012-08-24 14:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Checks that all expected matches have been found.
|
2015-04-11 10:00:23 +08:00
|
|
|
~ExpectedLocationVisitor() override {
|
2012-08-24 14:59:51 +08:00
|
|
|
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;
|
|
|
|
|
2017-10-12 07:54:34 +08:00
|
|
|
MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
|
|
|
|
: ExpectedName(Name.str()), LineNumber(LineNumber),
|
|
|
|
ColumnNumber(ColumnNumber) {
|
|
|
|
}
|
2012-08-24 14:59:51 +08:00
|
|
|
|
|
|
|
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 {
|
2017-10-12 07:54:34 +08:00
|
|
|
ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber,
|
2016-08-17 22:59:53 +08:00
|
|
|
unsigned Times)
|
|
|
|
: Candidate(Name, LineNumber, ColumnNumber), TimesExpected(Times),
|
|
|
|
TimesSeen(0) {}
|
2012-08-24 14:59:51 +08:00
|
|
|
|
|
|
|
void UpdateFor(StringRef Name, FullSourceLoc Location, SourceManager &SM) {
|
|
|
|
if (Candidate.Matches(Name, Location)) {
|
2016-08-17 22:59:53 +08:00
|
|
|
EXPECT_LT(TimesSeen, TimesExpected);
|
|
|
|
++TimesSeen;
|
|
|
|
} else if (TimesSeen < TimesExpected &&
|
|
|
|
Candidate.PartiallyMatches(Name, Location)) {
|
2012-08-24 14:59:51 +08:00
|
|
|
llvm::raw_string_ostream Stream(PartialMatches);
|
|
|
|
Stream << ", partial match: \"" << Name << "\" at ";
|
|
|
|
Location.print(Stream, SM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectFound() const {
|
2016-08-17 22:59:53 +08:00
|
|
|
EXPECT_EQ(TimesExpected, TimesSeen)
|
2012-08-24 14:59:51 +08:00
|
|
|
<< "Expected \"" << Candidate.ExpectedName
|
|
|
|
<< "\" at " << Candidate.LineNumber
|
|
|
|
<< ":" << Candidate.ColumnNumber << PartialMatches;
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchCandidate Candidate;
|
|
|
|
std::string PartialMatches;
|
2016-08-17 22:59:53 +08:00
|
|
|
unsigned TimesExpected;
|
|
|
|
unsigned TimesSeen;
|
2012-08-24 14:59:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<MatchCandidate> DisallowedMatches;
|
|
|
|
std::vector<ExpectedMatch> ExpectedMatches;
|
2012-06-25 07:56:26 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-06-25 07:56:26 +08:00
|
|
|
|
2014-10-09 23:02:06 +08:00
|
|
|
#endif
|