Rename APIs in unittests/AST/Language.h in preparation to share them

Summary:
Declaring these helpers in the ast_matcher namespace in the clangAST
unit test seems inappropriate -- neither these helpers, nor clangAST have
anything to do with AST matchers. Therefore, I moved these helpers to
the clang namespace.

Declaring another typedef called "ArgVector" is not a good idea -- we
already have both "ArgVector", "ArgsVector", and "ArgList". I expanded
it into the underlying type.

Declaring another enum called "Language" is not a good idea because we
arleady have the "clang::Language" enum. I renamed it to
"TestLanguage".

Similarly, I renamed "getBasicRunOptionsForLanguage" to
"getCommandLineArgsForTesting" to explain the semantics better (what are
"run options"?) and not repeat types in the function name
("ForLanguage").

Reviewers: shafik, rengolin, sammccall

Reviewed By: sammccall

Subscribers: gribozavr2, sammccall, martong, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80786
This commit is contained in:
Dmitri Gribenko 2020-05-29 14:12:51 +02:00
parent ab4d02cf26
commit d4ef654673
9 changed files with 141 additions and 130 deletions

View File

@ -38,7 +38,8 @@ void createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName,
llvm::MemoryBuffer::getMemBuffer(Code));
}
ASTImporterTestBase::TU::TU(StringRef Code, StringRef FileName, ArgVector Args,
ASTImporterTestBase::TU::TU(StringRef Code, StringRef FileName,
std::vector<std::string> Args,
ImporterConstructor C,
ASTImporter::ODRHandlingType ODRHandling)
: Code(std::string(Code)), FileName(std::string(FileName)),
@ -112,11 +113,12 @@ void ASTImporterTestBase::lazyInitSharedState(TranslationUnitDecl *ToTU) {
SharedStatePtr = std::make_shared<ASTImporterSharedState>(*ToTU);
}
void ASTImporterTestBase::lazyInitToAST(Language ToLang, StringRef ToSrcCode,
void ASTImporterTestBase::lazyInitToAST(TestLanguage ToLang,
StringRef ToSrcCode,
StringRef FileName) {
if (ToAST)
return;
ArgVector ToArgs = getArgVectorForLanguage(ToLang);
std::vector<std::string> ToArgs = getCommandLineArgsForLanguage(ToLang);
// Source code must be a valid live buffer through the tests lifetime.
ToCode = std::string(ToSrcCode);
// Build the AST from an empty file.
@ -136,12 +138,11 @@ ASTImporterTestBase::TU *ASTImporterTestBase::findFromTU(Decl *From) {
return &*It;
}
std::tuple<Decl *, Decl *>
ASTImporterTestBase::getImportedDecl(StringRef FromSrcCode, Language FromLang,
StringRef ToSrcCode, Language ToLang,
StringRef Identifier) {
ArgVector FromArgs = getArgVectorForLanguage(FromLang),
ToArgs = getArgVectorForLanguage(ToLang);
std::tuple<Decl *, Decl *> ASTImporterTestBase::getImportedDecl(
StringRef FromSrcCode, TestLanguage FromLang, StringRef ToSrcCode,
TestLanguage ToLang, StringRef Identifier) {
std::vector<std::string> FromArgs = getCommandLineArgsForLanguage(FromLang);
std::vector<std::string> ToArgs = getCommandLineArgsForLanguage(ToLang);
FromTUs.emplace_back(FromSrcCode, InputFileName, FromArgs, Creator,
ODRHandling);
@ -170,13 +171,13 @@ ASTImporterTestBase::getImportedDecl(StringRef FromSrcCode, Language FromLang,
}
TranslationUnitDecl *ASTImporterTestBase::getTuDecl(StringRef SrcCode,
Language Lang,
TestLanguage Lang,
StringRef FileName) {
assert(llvm::find_if(FromTUs, [FileName](const TU &E) {
return E.FileName == FileName;
}) == FromTUs.end());
ArgVector Args = getArgVectorForLanguage(Lang);
std::vector<std::string> Args = getCommandLineArgsForLanguage(Lang);
FromTUs.emplace_back(SrcCode, FileName, Args, Creator, ODRHandling);
TU &Tu = FromTUs.back();
@ -184,14 +185,14 @@ TranslationUnitDecl *ASTImporterTestBase::getTuDecl(StringRef SrcCode,
}
TranslationUnitDecl *ASTImporterTestBase::getToTuDecl(StringRef ToSrcCode,
Language ToLang) {
ArgVector ToArgs = getArgVectorForLanguage(ToLang);
TestLanguage ToLang) {
std::vector<std::string> ToArgs = getCommandLineArgsForLanguage(ToLang);
assert(!ToAST);
lazyInitToAST(ToLang, ToSrcCode, OutputFileName);
return ToAST->getASTContext().getTranslationUnitDecl();
}
Decl *ASTImporterTestBase::Import(Decl *From, Language ToLang) {
Decl *ASTImporterTestBase::Import(Decl *From, TestLanguage ToLang) {
lazyInitToAST(ToLang, "", OutputFileName);
TU *FromTU = findFromTU(From);
assert(SharedStatePtr);
@ -200,7 +201,7 @@ Decl *ASTImporterTestBase::Import(Decl *From, Language ToLang) {
}
llvm::Expected<Decl *> ASTImporterTestBase::importOrError(Decl *From,
Language ToLang) {
TestLanguage ToLang) {
lazyInitToAST(ToLang, "", OutputFileName);
TU *FromTU = findFromTU(From);
assert(SharedStatePtr);
@ -210,7 +211,7 @@ llvm::Expected<Decl *> ASTImporterTestBase::importOrError(Decl *From,
}
QualType ASTImporterTestBase::ImportType(QualType FromType, Decl *TUDecl,
Language ToLang) {
TestLanguage ToLang) {
lazyInitToAST(ToLang, "", OutputFileName);
TU *FromTU = findFromTU(TUDecl);
assert(SharedStatePtr);

View File

@ -52,13 +52,14 @@ void createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName,
class CompilerOptionSpecificTest : public ::testing::Test {
protected:
// Return the extra arguments appended to runtime options at compilation.
virtual ArgVector getExtraArgs() const { return ArgVector(); }
virtual std::vector<std::string> getExtraArgs() const { return {}; }
// Returns the argument vector used for a specific language option, this set
// can be tweaked by the test parameters.
ArgVector getArgVectorForLanguage(Language Lang) const {
ArgVector Args = getBasicRunOptionsForLanguage(Lang);
ArgVector ExtraArgs = getExtraArgs();
std::vector<std::string>
getCommandLineArgsForLanguage(TestLanguage Lang) const {
std::vector<std::string> Args = getCommandLineArgsForTesting(Lang);
std::vector<std::string> ExtraArgs = getExtraArgs();
for (const auto &Arg : ExtraArgs) {
Args.push_back(Arg);
}
@ -66,10 +67,13 @@ protected:
}
};
const auto DefaultTestArrayForRunOptions = std::array<ArgVector, 4>{
{ArgVector(), ArgVector{"-fdelayed-template-parsing"},
ArgVector{"-fms-compatibility"},
ArgVector{"-fdelayed-template-parsing", "-fms-compatibility"}}};
const auto DefaultTestArrayForRunOptions =
std::array<std::vector<std::string>, 4>{
{std::vector<std::string>(),
std::vector<std::string>{"-fdelayed-template-parsing"},
std::vector<std::string>{"-fms-compatibility"},
std::vector<std::string>{"-fdelayed-template-parsing",
"-fms-compatibility"}}};
const auto DefaultTestValuesForRunOptions =
::testing::ValuesIn(DefaultTestArrayForRunOptions);
@ -111,7 +115,7 @@ private:
ImporterConstructor Creator;
ASTImporter::ODRHandlingType ODRHandling;
TU(StringRef Code, StringRef FileName, ArgVector Args,
TU(StringRef Code, StringRef FileName, std::vector<std::string> Args,
ImporterConstructor C = ImporterConstructor(),
ASTImporter::ODRHandlingType ODRHandling =
ASTImporter::ODRHandlingType::Conservative);
@ -141,7 +145,8 @@ private:
// Initialize the shared state if not initialized already.
void lazyInitSharedState(TranslationUnitDecl *ToTU);
void lazyInitToAST(Language ToLang, StringRef ToSrcCode, StringRef FileName);
void lazyInitToAST(TestLanguage ToLang, StringRef ToSrcCode,
StringRef FileName);
protected:
std::shared_ptr<ASTImporterSharedState> SharedStatePtr;
@ -157,32 +162,33 @@ public:
// of the identifier into the To context.
// Must not be called more than once within the same test.
std::tuple<Decl *, Decl *>
getImportedDecl(StringRef FromSrcCode, Language FromLang, StringRef ToSrcCode,
Language ToLang, StringRef Identifier = DeclToImportID);
getImportedDecl(StringRef FromSrcCode, TestLanguage FromLang,
StringRef ToSrcCode, TestLanguage ToLang,
StringRef Identifier = DeclToImportID);
// Creates a TU decl for the given source code which can be used as a From
// context. May be called several times in a given test (with different file
// name).
TranslationUnitDecl *getTuDecl(StringRef SrcCode, Language Lang,
TranslationUnitDecl *getTuDecl(StringRef SrcCode, TestLanguage Lang,
StringRef FileName = "input.cc");
// Creates the To context with the given source code and returns the TU decl.
TranslationUnitDecl *getToTuDecl(StringRef ToSrcCode, Language ToLang);
TranslationUnitDecl *getToTuDecl(StringRef ToSrcCode, TestLanguage ToLang);
// Import the given Decl into the ToCtx.
// May be called several times in a given test.
// The different instances of the param From may have different ASTContext.
Decl *Import(Decl *From, Language ToLang);
Decl *Import(Decl *From, TestLanguage ToLang);
template <class DeclT> DeclT *Import(DeclT *From, Language Lang) {
template <class DeclT> DeclT *Import(DeclT *From, TestLanguage Lang) {
return cast_or_null<DeclT>(Import(cast<Decl>(From), Lang));
}
// Import the given Decl into the ToCtx.
// Same as Import but returns the result of the import which can be an error.
llvm::Expected<Decl *> importOrError(Decl *From, Language ToLang);
llvm::Expected<Decl *> importOrError(Decl *From, TestLanguage ToLang);
QualType ImportType(QualType FromType, Decl *TUDecl, Language ToLang);
QualType ImportType(QualType FromType, Decl *TUDecl, TestLanguage ToLang);
ASTImporterTestBase()
: ODRHandling(ASTImporter::ODRHandlingType::Conservative) {}
@ -191,9 +197,9 @@ public:
class ASTImporterOptionSpecificTestBase
: public ASTImporterTestBase,
public ::testing::WithParamInterface<ArgVector> {
public ::testing::WithParamInterface<std::vector<std::string>> {
protected:
ArgVector getExtraArgs() const override { return GetParam(); }
std::vector<std::string> getExtraArgs() const override { return GetParam(); }
};
template <class T>

View File

@ -37,7 +37,7 @@ struct Function {
BindableMatcher<Decl> getPattern() {
return functionDecl(hasName("X"), unless(isImplicit()));
}
Language getLang() { return Lang_C; }
TestLanguage getLang() { return Lang_C; }
};
struct Typedef {
@ -45,7 +45,7 @@ struct Typedef {
static constexpr auto *Definition = "typedef int X;";
static constexpr auto *ConflictingDefinition = "typedef double X;";
BindableMatcher<Decl> getPattern() { return typedefNameDecl(hasName("X")); }
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
struct TypedefAlias {
@ -53,7 +53,7 @@ struct TypedefAlias {
static constexpr auto *Definition = "using X = int;";
static constexpr auto *ConflictingDefinition = "using X = double;";
BindableMatcher<Decl> getPattern() { return typedefNameDecl(hasName("X")); }
Language getLang() { return Lang_CXX11; }
TestLanguage getLang() { return Lang_CXX11; }
};
struct Enum {
@ -61,7 +61,7 @@ struct Enum {
static constexpr auto *Definition = "enum X { a, b };";
static constexpr auto *ConflictingDefinition = "enum X { a, b, c };";
BindableMatcher<Decl> getPattern() { return enumDecl(hasName("X")); }
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
struct EnumClass {
@ -69,7 +69,7 @@ struct EnumClass {
static constexpr auto *Definition = "enum class X { a, b };";
static constexpr auto *ConflictingDefinition = "enum class X { a, b, c };";
BindableMatcher<Decl> getPattern() { return enumDecl(hasName("X")); }
Language getLang() { return Lang_CXX11; }
TestLanguage getLang() { return Lang_CXX11; }
};
struct EnumConstant {
@ -77,7 +77,7 @@ struct EnumConstant {
static constexpr auto *Definition = "enum E { X = 0 };";
static constexpr auto *ConflictingDefinition = "enum E { X = 1 };";
BindableMatcher<Decl> getPattern() { return enumConstantDecl(hasName("X")); }
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
struct Class {
@ -88,7 +88,7 @@ struct Class {
BindableMatcher<Decl> getPattern() {
return cxxRecordDecl(hasName("X"), unless(isImplicit()));
}
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
struct Variable {
@ -98,7 +98,7 @@ struct Variable {
static constexpr auto *Definition = "int X;";
static constexpr auto *ConflictingDefinition = "float X;";
BindableMatcher<Decl> getPattern() { return varDecl(hasName("X")); }
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
struct ClassTemplate {
@ -112,7 +112,7 @@ struct ClassTemplate {
BindableMatcher<Decl> getPattern() {
return classTemplateDecl(hasName("X"), unless(isImplicit()));
}
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
struct FunctionTemplate {
@ -133,7 +133,7 @@ struct FunctionTemplate {
}
static std::string getDef0() { return Definition0; }
static std::string getDef1() { return Definition1; }
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
static const internal::VariadicDynCastAllOfMatcher<Decl, VarTemplateDecl>
@ -152,7 +152,7 @@ struct VarTemplate {
constexpr int X = 0;
)";
BindableMatcher<Decl> getPattern() { return varTemplateDecl(hasName("X")); }
Language getLang() { return Lang_CXX14; }
TestLanguage getLang() { return Lang_CXX14; }
};
struct ClassTemplateSpec {
@ -175,7 +175,7 @@ struct ClassTemplateSpec {
BindableMatcher<Decl> getPattern() {
return classTemplateSpecializationDecl(hasName("X"), unless(isImplicit()));
}
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
// Function template specializations are all "full" specializations.
@ -208,7 +208,7 @@ struct FunctionTemplateSpec {
}
static std::string getDef0() { return Definition0; }
static std::string getDef1() { return Definition1; }
Language getLang() { return Lang_CXX; }
TestLanguage getLang() { return Lang_CXX; }
};
static const internal::VariadicDynCastAllOfMatcher<
@ -230,7 +230,7 @@ struct VarTemplateSpec {
BindableMatcher<Decl> getPattern() {
return varTemplateSpecializationDecl(hasName("X"), unless(isImplicit()));
}
Language getLang() { return Lang_CXX14; }
TestLanguage getLang() { return Lang_CXX14; }
};
template <typename TypeParam, ASTImporter::ODRHandlingType ODRHandlingParam>
@ -252,7 +252,7 @@ struct ODRViolation : ASTImporterOptionSpecificTestBase {
return TypeParam::ConflictingProtoDef;
}
static BindableMatcher<Decl> getPattern() { return TypeParam().getPattern(); }
static Language getLang() { return TypeParam().getLang(); }
static TestLanguage getLang() { return TypeParam().getLang(); }
template <std::string (*ToTUContent)(), std::string (*FromTUContent)(),
void (*ResultChecker)(llvm::Expected<Decl *> &, Decl *, Decl *)>

View File

@ -28,8 +28,9 @@ using internal::BindableMatcher;
using llvm::StringMap;
// Base class for those tests which use the family of `testImport` functions.
class TestImportBase : public CompilerOptionSpecificTest,
public ::testing::WithParamInterface<ArgVector> {
class TestImportBase
: public CompilerOptionSpecificTest,
public ::testing::WithParamInterface<std::vector<std::string>> {
template <typename NodeType>
llvm::Expected<NodeType> importNode(ASTUnit *From, ASTUnit *To,
@ -62,8 +63,9 @@ class TestImportBase : public CompilerOptionSpecificTest,
template <typename NodeType>
testing::AssertionResult
testImport(const std::string &FromCode, const ArgVector &FromArgs,
const std::string &ToCode, const ArgVector &ToArgs,
testImport(const std::string &FromCode,
const std::vector<std::string> &FromArgs,
const std::string &ToCode, const std::vector<std::string> &ToArgs,
MatchVerifier<NodeType> &Verifier,
const BindableMatcher<NodeType> &SearchMatcher,
const BindableMatcher<NodeType> &VerificationMatcher) {
@ -110,8 +112,9 @@ class TestImportBase : public CompilerOptionSpecificTest,
template <typename NodeType>
testing::AssertionResult
testImport(const std::string &FromCode, const ArgVector &FromArgs,
const std::string &ToCode, const ArgVector &ToArgs,
testImport(const std::string &FromCode,
const std::vector<std::string> &FromArgs,
const std::string &ToCode, const std::vector<std::string> &ToArgs,
MatchVerifier<NodeType> &Verifier,
const BindableMatcher<NodeType> &VerificationMatcher) {
return testImport(
@ -122,7 +125,7 @@ class TestImportBase : public CompilerOptionSpecificTest,
}
protected:
ArgVector getExtraArgs() const override { return GetParam(); }
std::vector<std::string> getExtraArgs() const override { return GetParam(); }
public:
@ -130,12 +133,12 @@ public:
/// of "FromCode" virtual file is imported to "ToCode" virtual file.
/// The verification is done by running AMatcher over the imported node.
template <typename NodeType, typename MatcherType>
void testImport(const std::string &FromCode, Language FromLang,
const std::string &ToCode, Language ToLang,
void testImport(const std::string &FromCode, TestLanguage FromLang,
const std::string &ToCode, TestLanguage ToLang,
MatchVerifier<NodeType> &Verifier,
const MatcherType &AMatcher) {
ArgVector FromArgs = getArgVectorForLanguage(FromLang),
ToArgs = getArgVectorForLanguage(ToLang);
std::vector<std::string> FromArgs = getCommandLineArgsForLanguage(FromLang);
std::vector<std::string> ToArgs = getCommandLineArgsForLanguage(ToLang);
EXPECT_TRUE(
testImport(FromCode, FromArgs, ToCode, ToArgs, Verifier, AMatcher));
}
@ -162,14 +165,14 @@ public:
struct CodeEntry {
std::string CodeSample;
Language Lang;
TestLanguage Lang;
};
using CodeFiles = StringMap<CodeEntry>;
/// Builds an ASTUnit for one potential compile options set.
SingleASTUnit createASTUnit(StringRef FileName, const CodeEntry &CE) const {
ArgVector Args = getArgVectorForLanguage(CE.Lang);
std::vector<std::string> Args = getCommandLineArgsForLanguage(CE.Lang);
auto AST = tooling::buildASTFromCodeWithArgs(CE.CodeSample, Args, FileName);
EXPECT_TRUE(AST.get());
return AST;
@ -5523,14 +5526,14 @@ TEST_P(ASTImporterOptionSpecificTestBase,
}
INSTANTIATE_TEST_CASE_P(ParameterizedTests, SVEBuiltins,
::testing::Values(ArgVector{"-target",
"aarch64-linux-gnu"}), );
::testing::Values(std::vector<std::string>{
"-target", "aarch64-linux-gnu"}), );
INSTANTIATE_TEST_CASE_P(ParameterizedTests, DeclContextTest,
::testing::Values(ArgVector()), );
::testing::Values(std::vector<std::string>()), );
INSTANTIATE_TEST_CASE_P(ParameterizedTests, CanonicalRedeclChain,
::testing::Values(ArgVector()), );
::testing::Values(std::vector<std::string>()), );
TEST_P(ASTImporterOptionSpecificTestBase, LambdasAreDifferentiated) {
Decl *FromTU = getTuDecl(
@ -5982,9 +5985,9 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
}
template <typename T>
auto ExtendWithOptions(const T &Values, const ArgVector &Args) {
auto ExtendWithOptions(const T &Values, const std::vector<std::string> &Args) {
auto Copy = Values;
for (ArgVector &ArgV : Copy) {
for (std::vector<std::string> &ArgV : Copy) {
for (const std::string &Arg : Args) {
ArgV.push_back(Arg);
}
@ -6056,14 +6059,15 @@ INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions, );
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportPath,
::testing::Values(ArgVector()), );
::testing::Values(std::vector<std::string>()), );
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportExpr,
DefaultTestValuesForRunOptions, );
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFixedPointExpr,
ExtendWithOptions(DefaultTestArrayForRunOptions,
ArgVector{"-ffixed-point"}), );
std::vector<std::string>{
"-ffixed-point"}), );
INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportType,
DefaultTestValuesForRunOptions, );

View File

@ -96,8 +96,8 @@ const auto *AnonCT = "namespace { template <class> class X; }";
// First value in tuple: Compile options.
// Second value in tuple: Source code to be used in the test.
using ImportVisibilityChainParams =
::testing::WithParamInterface<std::tuple<ArgVector, const char *>>;
using ImportVisibilityChainParams = ::testing::WithParamInterface<
std::tuple<std::vector<std::string>, const char *>>;
// Fixture to test the redecl chain of Decls with the same visibility. Gtest
// makes it possible to have either value-parameterized or type-parameterized
// fixtures. However, we cannot have both value- and type-parameterized test
@ -109,7 +109,9 @@ class ImportVisibilityChain
: public ASTImporterTestBase, public ImportVisibilityChainParams {
protected:
using DeclTy = typename PatternFactory::DeclTy;
ArgVector getExtraArgs() const override { return std::get<0>(GetParam()); }
std::vector<std::string> getExtraArgs() const override {
return std::get<0>(GetParam());
}
std::string getCode() const { return std::get<1>(GetParam()); }
BindableMatcher<Decl> getPattern() const { return PatternFactory()(); }
@ -222,8 +224,8 @@ INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportClassTemplatesVisibilityChain,
// functions are expected to be linked in a declaration chain.
// One value of this tuple is combined with every value of compile options.
// The test can have a single tuple as parameter only.
using ImportVisibilityParams = ::testing::WithParamInterface<
std::tuple<ArgVector, std::tuple<const char *, const char *, bool>>>;
using ImportVisibilityParams = ::testing::WithParamInterface<std::tuple<
std::vector<std::string>, std::tuple<const char *, const char *, bool>>>;
template <typename PatternFactory>
class ImportVisibility
@ -231,7 +233,9 @@ class ImportVisibility
public ImportVisibilityParams {
protected:
using DeclTy = typename PatternFactory::DeclTy;
ArgVector getExtraArgs() const override { return std::get<0>(GetParam()); }
std::vector<std::string> getExtraArgs() const override {
return std::get<0>(GetParam());
}
std::string getCode0() const { return std::get<0>(std::get<1>(GetParam())); }
std::string getCode1() const { return std::get<1>(std::get<1>(GetParam())); }
bool shouldBeLinked() const { return std::get<2>(std::get<1>(GetParam())); }

View File

@ -11,43 +11,42 @@
//===----------------------------------------------------------------------===//
#include "Language.h"
#include "llvm/Support/ErrorHandling.h"
namespace clang {
namespace ast_matchers {
ArgVector getBasicRunOptionsForLanguage(Language Lang) {
ArgVector BasicArgs;
std::vector<std::string> getCommandLineArgsForTesting(TestLanguage Lang) {
std::vector<std::string> Args;
// Test with basic arguments.
switch (Lang) {
case Lang_C:
BasicArgs = {"-x", "c", "-std=c99"};
Args = {"-x", "c", "-std=c99"};
break;
case Lang_C89:
BasicArgs = {"-x", "c", "-std=c89"};
Args = {"-x", "c", "-std=c89"};
break;
case Lang_CXX:
BasicArgs = {"-std=c++98", "-frtti"};
Args = {"-std=c++98", "-frtti"};
break;
case Lang_CXX11:
BasicArgs = {"-std=c++11", "-frtti"};
Args = {"-std=c++11", "-frtti"};
break;
case Lang_CXX14:
BasicArgs = {"-std=c++14", "-frtti"};
Args = {"-std=c++14", "-frtti"};
break;
case Lang_CXX17:
BasicArgs = {"-std=c++17", "-frtti"};
Args = {"-std=c++17", "-frtti"};
break;
case Lang_CXX2a:
BasicArgs = {"-std=c++2a", "-frtti"};
Args = {"-std=c++2a", "-frtti"};
break;
case Lang_OBJCXX:
BasicArgs = {"-x", "objective-c++", "-frtti"};
Args = {"-x", "objective-c++", "-frtti"};
break;
case Lang_OpenCL:
llvm_unreachable("Not implemented yet!");
}
return BasicArgs;
return Args;
}
} // end namespace ast_matchers
} // end namespace clang

View File

@ -13,30 +13,25 @@
#ifndef LLVM_CLANG_UNITTESTS_AST_LANGUAGE_H
#define LLVM_CLANG_UNITTESTS_AST_LANGUAGE_H
#include "llvm/Support/ErrorHandling.h"
#include <vector>
#include <string>
namespace clang {
namespace ast_matchers {
typedef std::vector<std::string> ArgVector;
enum Language {
Lang_C,
Lang_C89,
Lang_CXX,
Lang_CXX11,
Lang_CXX14,
Lang_CXX17,
Lang_CXX2a,
Lang_OpenCL,
Lang_OBJCXX
enum TestLanguage {
Lang_C,
Lang_C89,
Lang_CXX,
Lang_CXX11,
Lang_CXX14,
Lang_CXX17,
Lang_CXX2a,
Lang_OpenCL,
Lang_OBJCXX
};
ArgVector getBasicRunOptionsForLanguage(Language Lang);
std::vector<std::string> getCommandLineArgsForTesting(TestLanguage Lang);
} // end namespace ast_matchers
} // end namespace clang
#endif

View File

@ -41,17 +41,15 @@ public:
template <typename MatcherType>
testing::AssertionResult match(const std::string &Code,
const MatcherType &AMatcher,
Language L) {
const MatcherType &AMatcher, TestLanguage L) {
std::vector<std::string> Args;
return match(Code, AMatcher, Args, L);
}
template <typename MatcherType>
testing::AssertionResult match(const std::string &Code,
const MatcherType &AMatcher,
std::vector<std::string>& Args,
Language L);
testing::AssertionResult
match(const std::string &Code, const MatcherType &AMatcher,
std::vector<std::string> &Args, TestLanguage L);
template <typename MatcherType>
testing::AssertionResult match(const Decl *D, const MatcherType &AMatcher);
@ -77,10 +75,12 @@ private:
/// \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,
std::vector<std::string>& Args, Language L) {
template <typename NodeType>
template <typename MatcherType>
testing::AssertionResult
MatchVerifier<NodeType>::match(const std::string &Code,
const MatcherType &AMatcher,
std::vector<std::string> &Args, TestLanguage L) {
MatchFinder Finder;
Finder.addMatcher(AMatcher.bind(""), this);
std::unique_ptr<tooling::FrontendActionFactory> Factory(

View File

@ -23,12 +23,13 @@ struct StructuralEquivalenceTest : ::testing::Test {
// snippets. To determine the returned node, a separate matcher is specified
// for both snippets. The first matching node is returned.
template <typename NodeType, typename MatcherType>
std::tuple<NodeType *, NodeType *> makeDecls(
const std::string &SrcCode0, const std::string &SrcCode1, Language Lang,
const MatcherType &Matcher0, const MatcherType &Matcher1) {
std::tuple<NodeType *, NodeType *>
makeDecls(const std::string &SrcCode0, const std::string &SrcCode1,
TestLanguage Lang, const MatcherType &Matcher0,
const MatcherType &Matcher1) {
this->Code0 = SrcCode0;
this->Code1 = SrcCode1;
ArgVector Args = getBasicRunOptionsForLanguage(Lang);
std::vector<std::string> Args = getCommandLineArgsForTesting(Lang);
const char *const InputFileName = "input.cc";
@ -43,11 +44,12 @@ struct StructuralEquivalenceTest : ::testing::Test {
return std::make_tuple(D0, D1);
}
std::tuple<TranslationUnitDecl *, TranslationUnitDecl *> makeTuDecls(
const std::string &SrcCode0, const std::string &SrcCode1, Language Lang) {
std::tuple<TranslationUnitDecl *, TranslationUnitDecl *>
makeTuDecls(const std::string &SrcCode0, const std::string &SrcCode1,
TestLanguage Lang) {
this->Code0 = SrcCode0;
this->Code1 = SrcCode1;
ArgVector Args = getBasicRunOptionsForLanguage(Lang);
std::vector<std::string> Args = getCommandLineArgsForTesting(Lang);
const char *const InputFileName = "input.cc";
@ -61,9 +63,9 @@ struct StructuralEquivalenceTest : ::testing::Test {
// Get a pair of node pointers into the synthesized AST from the given code
// snippets. The same matcher is used for both snippets.
template <typename NodeType, typename MatcherType>
std::tuple<NodeType *, NodeType *> makeDecls(
const std::string &SrcCode0, const std::string &SrcCode1, Language Lang,
const MatcherType &AMatcher) {
std::tuple<NodeType *, NodeType *>
makeDecls(const std::string &SrcCode0, const std::string &SrcCode1,
TestLanguage Lang, const MatcherType &AMatcher) {
return makeDecls<NodeType, MatcherType>(
SrcCode0, SrcCode1, Lang, AMatcher, AMatcher);
}
@ -71,9 +73,9 @@ struct StructuralEquivalenceTest : ::testing::Test {
// Get a pair of Decl pointers to the synthesized declarations from the given
// code snippets. We search for the first NamedDecl with given name in both
// snippets.
std::tuple<NamedDecl *, NamedDecl *> makeNamedDecls(
const std::string &SrcCode0, const std::string &SrcCode1,
Language Lang, const char *const Identifier = "foo") {
std::tuple<NamedDecl *, NamedDecl *>
makeNamedDecls(const std::string &SrcCode0, const std::string &SrcCode1,
TestLanguage Lang, const char *const Identifier = "foo") {
auto Matcher = namedDecl(hasName(Identifier));
return makeDecls<NamedDecl>(SrcCode0, SrcCode1, Lang, Matcher);
}