2012-07-06 13:48:52 +08:00
|
|
|
//===- unittest/Tooling/ASTMatchersTest.h - Matcher tests helpers ------===//
|
|
|
|
//
|
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-07-06 13:48:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_UNITTESTS_ASTMATCHERS_ASTMATCHERSTEST_H
|
|
|
|
#define LLVM_CLANG_UNITTESTS_ASTMATCHERS_ASTMATCHERSTEST_H
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2013-11-08 06:30:36 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2012-07-06 13:48:52 +08:00
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
|
2013-11-08 06:30:36 +08:00
|
|
|
using clang::tooling::buildASTFromCodeWithArgs;
|
2012-07-06 13:48:52 +08:00
|
|
|
using clang::tooling::newFrontendActionFactory;
|
2012-08-30 10:08:31 +08:00
|
|
|
using clang::tooling::runToolOnCodeWithArgs;
|
2012-07-06 13:48:52 +08:00
|
|
|
using clang::tooling::FrontendActionFactory;
|
2014-11-26 01:01:06 +08:00
|
|
|
using clang::tooling::FileContentMappings;
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
class BoundNodesCallback {
|
|
|
|
public:
|
2015-10-20 21:23:58 +08:00
|
|
|
virtual ~BoundNodesCallback() {}
|
2012-10-29 18:48:25 +08:00
|
|
|
virtual bool run(const BoundNodes *BoundNodes) = 0;
|
|
|
|
virtual bool run(const BoundNodes *BoundNodes, ASTContext *Context) = 0;
|
2013-11-08 06:30:32 +08:00
|
|
|
virtual void onEndOfTranslationUnit() {}
|
2012-07-06 13:48:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// If 'FindResultVerifier' is not NULL, sets *Verified to the result of
|
|
|
|
// running 'FindResultVerifier' with the bound nodes as argument.
|
|
|
|
// If 'FindResultVerifier' is NULL, sets *Verified to true when Run is called.
|
|
|
|
class VerifyMatch : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
2016-04-21 21:51:07 +08:00
|
|
|
VerifyMatch(std::unique_ptr<BoundNodesCallback> FindResultVerifier, bool *Verified)
|
|
|
|
: Verified(Verified), FindResultReviewer(std::move(FindResultVerifier)) {}
|
2012-07-06 13:48:52 +08:00
|
|
|
|
2015-04-09 01:01:55 +08:00
|
|
|
void run(const MatchFinder::MatchResult &Result) override {
|
2014-06-08 16:38:12 +08:00
|
|
|
if (FindResultReviewer != nullptr) {
|
2012-11-12 06:14:55 +08:00
|
|
|
*Verified |= FindResultReviewer->run(&Result.Nodes, Result.Context);
|
2012-07-06 13:48:52 +08:00
|
|
|
} else {
|
|
|
|
*Verified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 17:32:10 +08:00
|
|
|
void onEndOfTranslationUnit() override {
|
2013-11-08 06:30:32 +08:00
|
|
|
if (FindResultReviewer)
|
|
|
|
FindResultReviewer->onEndOfTranslationUnit();
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
private:
|
|
|
|
bool *const Verified;
|
2016-04-21 21:51:07 +08:00
|
|
|
const std::unique_ptr<BoundNodesCallback> FindResultReviewer;
|
2012-07-06 13:48:52 +08:00
|
|
|
};
|
|
|
|
|
2019-06-13 21:48:24 +08:00
|
|
|
enum class LanguageMode {
|
|
|
|
Cxx11,
|
|
|
|
Cxx14,
|
|
|
|
Cxx17,
|
|
|
|
Cxx2a,
|
|
|
|
Cxx11OrLater,
|
|
|
|
Cxx14OrLater,
|
|
|
|
Cxx17OrLater,
|
|
|
|
Cxx2aOrLater
|
|
|
|
};
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
template <typename T>
|
2014-11-26 01:01:06 +08:00
|
|
|
testing::AssertionResult matchesConditionally(
|
2020-06-03 04:20:58 +08:00
|
|
|
const Twine &Code, const T &AMatcher, bool ExpectMatch,
|
2017-03-16 04:14:25 +08:00
|
|
|
llvm::ArrayRef<llvm::StringRef> CompileArgs,
|
2015-03-12 23:48:15 +08:00
|
|
|
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
|
2020-06-03 04:20:58 +08:00
|
|
|
StringRef Filename = "input.cc") {
|
2013-11-08 06:30:32 +08:00
|
|
|
bool Found = false, DynamicFound = false;
|
2012-07-06 13:48:52 +08:00
|
|
|
MatchFinder Finder;
|
2014-06-08 16:38:12 +08:00
|
|
|
VerifyMatch VerifyFound(nullptr, &Found);
|
2014-04-24 11:30:22 +08:00
|
|
|
Finder.addMatcher(AMatcher, &VerifyFound);
|
2014-06-08 16:38:12 +08:00
|
|
|
VerifyMatch VerifyDynamicFound(nullptr, &DynamicFound);
|
2014-04-24 11:30:22 +08:00
|
|
|
if (!Finder.addDynamicMatcher(AMatcher, &VerifyDynamicFound))
|
2013-11-08 06:30:32 +08:00
|
|
|
return testing::AssertionFailure() << "Could not add dynamic matcher";
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<FrontendActionFactory> Factory(
|
|
|
|
newFrontendActionFactory(&Finder));
|
2016-07-01 02:12:25 +08:00
|
|
|
// Some tests need rtti/exceptions on. Use an unknown-unknown triple so we
|
|
|
|
// don't instantiate the full system toolchain. On Linux, instantiating the
|
|
|
|
// toolchain involves stat'ing large portions of /usr/lib, and this slows down
|
|
|
|
// not only this test, but all other tests, via contention in the kernel.
|
|
|
|
//
|
|
|
|
// FIXME: This is a hack to work around the fact that there's no way to do the
|
|
|
|
// equivalent of runToolOnCodeWithArgs without instantiating a full Driver.
|
|
|
|
// We should consider having a function, at least for tests, that invokes cc1.
|
2017-03-16 04:14:25 +08:00
|
|
|
std::vector<std::string> Args(CompileArgs.begin(), CompileArgs.end());
|
|
|
|
Args.insert(Args.end(), {"-frtti", "-fexceptions",
|
|
|
|
"-target", "i386-unknown-unknown"});
|
2016-01-29 19:29:02 +08:00
|
|
|
if (!runToolOnCodeWithArgs(
|
|
|
|
Factory->create(), Code, Args, Filename, "clang-tool",
|
|
|
|
std::make_shared<PCHContainerOperations>(), VirtualMappedFiles)) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
|
|
|
|
}
|
2013-11-08 06:30:32 +08:00
|
|
|
if (Found != DynamicFound) {
|
|
|
|
return testing::AssertionFailure() << "Dynamic match result ("
|
|
|
|
<< DynamicFound
|
|
|
|
<< ") does not match static result ("
|
|
|
|
<< Found << ")";
|
|
|
|
}
|
2012-07-06 13:48:52 +08:00
|
|
|
if (!Found && ExpectMatch) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Could not find match in \"" << Code << "\"";
|
|
|
|
} else if (Found && !ExpectMatch) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Found unexpected match in \"" << Code << "\"";
|
|
|
|
}
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
|
2017-03-16 04:14:25 +08:00
|
|
|
template <typename T>
|
|
|
|
testing::AssertionResult matchesConditionally(
|
2020-06-03 04:20:58 +08:00
|
|
|
const Twine &Code, const T &AMatcher, bool ExpectMatch,
|
2017-03-16 04:14:25 +08:00
|
|
|
llvm::StringRef CompileArg,
|
|
|
|
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
|
2020-06-03 04:20:58 +08:00
|
|
|
StringRef Filename = "input.cc") {
|
2017-03-16 04:14:25 +08:00
|
|
|
return matchesConditionally(Code, AMatcher, ExpectMatch,
|
|
|
|
llvm::makeArrayRef(CompileArg),
|
|
|
|
VirtualMappedFiles, Filename);
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
template <typename T>
|
2019-06-13 21:48:24 +08:00
|
|
|
testing::AssertionResult
|
2020-06-03 04:20:58 +08:00
|
|
|
matchesConditionally(const Twine &Code, const T &AMatcher, bool ExpectMatch,
|
|
|
|
const LanguageMode &Mode) {
|
2019-06-13 21:48:24 +08:00
|
|
|
std::vector<LanguageMode> LangModes;
|
|
|
|
switch (Mode) {
|
|
|
|
case LanguageMode::Cxx11:
|
|
|
|
case LanguageMode::Cxx14:
|
|
|
|
case LanguageMode::Cxx17:
|
|
|
|
case LanguageMode::Cxx2a:
|
|
|
|
LangModes = {Mode};
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx11OrLater:
|
|
|
|
LangModes = {LanguageMode::Cxx11, LanguageMode::Cxx14, LanguageMode::Cxx17,
|
|
|
|
LanguageMode::Cxx2a};
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx14OrLater:
|
|
|
|
LangModes = {LanguageMode::Cxx14, LanguageMode::Cxx17, LanguageMode::Cxx2a};
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx17OrLater:
|
|
|
|
LangModes = {LanguageMode::Cxx17, LanguageMode::Cxx2a};
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx2aOrLater:
|
|
|
|
LangModes = {LanguageMode::Cxx2a};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto Mode : LangModes) {
|
2020-06-01 14:52:01 +08:00
|
|
|
StringRef LangModeArg;
|
2019-06-13 21:48:24 +08:00
|
|
|
switch (Mode) {
|
|
|
|
case LanguageMode::Cxx11:
|
|
|
|
LangModeArg = "-std=c++11";
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx14:
|
|
|
|
LangModeArg = "-std=c++14";
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx17:
|
|
|
|
LangModeArg = "-std=c++17";
|
|
|
|
break;
|
|
|
|
case LanguageMode::Cxx2a:
|
|
|
|
LangModeArg = "-std=c++2a";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid language mode");
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:52:01 +08:00
|
|
|
auto Result = matchesConditionally(Code, AMatcher, ExpectMatch,
|
|
|
|
{LangModeArg, "-Werror=c++14-extensions",
|
|
|
|
"-Werror=c++17-extensions",
|
|
|
|
"-Werror=c++20-extensions"});
|
2019-06-13 21:48:24 +08:00
|
|
|
if (!Result)
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
testing::AssertionResult
|
2020-06-03 04:20:58 +08:00
|
|
|
matches(const Twine &Code, const T &AMatcher,
|
2019-06-13 21:48:24 +08:00
|
|
|
const LanguageMode &Mode = LanguageMode::Cxx11) {
|
|
|
|
return matchesConditionally(Code, AMatcher, true, Mode);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-06-13 21:48:24 +08:00
|
|
|
testing::AssertionResult
|
2020-06-03 04:20:58 +08:00
|
|
|
notMatches(const Twine &Code, const T &AMatcher,
|
2019-06-13 21:48:24 +08:00
|
|
|
const LanguageMode &Mode = LanguageMode::Cxx11) {
|
|
|
|
return matchesConditionally(Code, AMatcher, false, Mode);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 23:48:15 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchesObjC(const Twine &Code, const T &AMatcher,
|
2017-03-16 04:14:25 +08:00
|
|
|
bool ExpectMatch = true) {
|
|
|
|
return matchesConditionally(Code, AMatcher, ExpectMatch,
|
|
|
|
{"-fobjc-nonfragile-abi", "-Wno-objc-root-class",
|
2018-05-17 06:47:03 +08:00
|
|
|
"-fblocks", "-Wno-incomplete-implementation"},
|
2017-03-16 04:14:25 +08:00
|
|
|
FileContentMappings(), "input.m");
|
2015-03-12 23:48:15 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 02:34:48 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchesC(const Twine &Code, const T &AMatcher) {
|
2015-09-05 02:34:48 +08:00
|
|
|
return matchesConditionally(Code, AMatcher, true, "", FileContentMappings(),
|
|
|
|
"input.c");
|
|
|
|
}
|
|
|
|
|
Adding new AST matchers for: addrLabelExpr, atomicExpr, binaryConditionalOperator, designatedInitExpr, designatorCountIs, hasSyntacticForm, implicitValueInitExpr, labelDecl, opaqueValueExpr, parenListExpr, predefinedExpr, requiresZeroInitialization, and stmtExpr.
Patch by Aleksei Sidorin.
llvm-svn: 263027
2016-03-10 01:11:51 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchesC99(const Twine &Code, const T &AMatcher) {
|
Adding new AST matchers for: addrLabelExpr, atomicExpr, binaryConditionalOperator, designatedInitExpr, designatorCountIs, hasSyntacticForm, implicitValueInitExpr, labelDecl, opaqueValueExpr, parenListExpr, predefinedExpr, requiresZeroInitialization, and stmtExpr.
Patch by Aleksei Sidorin.
llvm-svn: 263027
2016-03-10 01:11:51 +08:00
|
|
|
return matchesConditionally(Code, AMatcher, true, "-std=c99",
|
|
|
|
FileContentMappings(), "input.c");
|
|
|
|
}
|
|
|
|
|
2015-10-05 22:41:27 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult notMatchesC(const Twine &Code, const T &AMatcher) {
|
2015-10-05 22:41:27 +08:00
|
|
|
return matchesConditionally(Code, AMatcher, false, "", FileContentMappings(),
|
|
|
|
"input.c");
|
|
|
|
}
|
|
|
|
|
2015-03-12 23:48:15 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult notMatchesObjC(const Twine &Code, const T &AMatcher) {
|
2017-03-16 04:14:25 +08:00
|
|
|
return matchesObjC(Code, AMatcher, false);
|
2015-03-12 23:48:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 17:45:53 +08:00
|
|
|
// Function based on matchesConditionally with "-x cuda" argument added and
|
|
|
|
// small CUDA header prepended to the code string.
|
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult
|
|
|
|
matchesConditionallyWithCuda(const Twine &Code, const T &AMatcher,
|
|
|
|
bool ExpectMatch, llvm::StringRef CompileArg) {
|
2014-08-05 17:45:53 +08:00
|
|
|
const std::string CudaHeader =
|
|
|
|
"typedef unsigned int size_t;\n"
|
|
|
|
"#define __constant__ __attribute__((constant))\n"
|
|
|
|
"#define __device__ __attribute__((device))\n"
|
|
|
|
"#define __global__ __attribute__((global))\n"
|
|
|
|
"#define __host__ __attribute__((host))\n"
|
|
|
|
"#define __shared__ __attribute__((shared))\n"
|
|
|
|
"struct dim3 {"
|
|
|
|
" unsigned x, y, z;"
|
|
|
|
" __host__ __device__ dim3(unsigned x, unsigned y = 1, unsigned z = 1)"
|
|
|
|
" : x(x), y(y), z(z) {}"
|
|
|
|
"};"
|
|
|
|
"typedef struct cudaStream *cudaStream_t;"
|
|
|
|
"int cudaConfigureCall(dim3 gridSize, dim3 blockSize,"
|
|
|
|
" size_t sharedSize = 0,"
|
2019-02-01 05:34:03 +08:00
|
|
|
" cudaStream_t stream = 0);"
|
|
|
|
"extern \"C\" unsigned __cudaPushCallConfiguration("
|
|
|
|
" dim3 gridDim, dim3 blockDim, size_t sharedMem = 0, void *stream = 0);";
|
2014-08-05 17:45:53 +08:00
|
|
|
|
|
|
|
bool Found = false, DynamicFound = false;
|
|
|
|
MatchFinder Finder;
|
|
|
|
VerifyMatch VerifyFound(nullptr, &Found);
|
|
|
|
Finder.addMatcher(AMatcher, &VerifyFound);
|
|
|
|
VerifyMatch VerifyDynamicFound(nullptr, &DynamicFound);
|
|
|
|
if (!Finder.addDynamicMatcher(AMatcher, &VerifyDynamicFound))
|
|
|
|
return testing::AssertionFailure() << "Could not add dynamic matcher";
|
|
|
|
std::unique_ptr<FrontendActionFactory> Factory(
|
|
|
|
newFrontendActionFactory(&Finder));
|
2016-07-01 02:12:25 +08:00
|
|
|
// Some tests use typeof, which is a gnu extension. Using an explicit
|
|
|
|
// unknown-unknown triple is good for a large speedup, because it lets us
|
|
|
|
// avoid constructing a full system triple.
|
|
|
|
std::vector<std::string> Args = {
|
2020-01-29 03:23:46 +08:00
|
|
|
"-xcuda", "-fno-ms-extensions", "--cuda-host-only", "-nocudainc",
|
|
|
|
"-target", "x86_64-unknown-unknown", std::string(CompileArg)};
|
2014-08-05 17:45:53 +08:00
|
|
|
if (!runToolOnCodeWithArgs(Factory->create(),
|
|
|
|
CudaHeader + Code, Args)) {
|
|
|
|
return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
|
|
|
|
}
|
|
|
|
if (Found != DynamicFound) {
|
|
|
|
return testing::AssertionFailure() << "Dynamic match result ("
|
|
|
|
<< DynamicFound
|
|
|
|
<< ") does not match static result ("
|
|
|
|
<< Found << ")";
|
|
|
|
}
|
|
|
|
if (!Found && ExpectMatch) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Could not find match in \"" << Code << "\"";
|
|
|
|
} else if (Found && !ExpectMatch) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Found unexpected match in \"" << Code << "\"";
|
|
|
|
}
|
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchesWithCuda(const Twine &Code, const T &AMatcher) {
|
2014-08-05 17:45:53 +08:00
|
|
|
return matchesConditionallyWithCuda(Code, AMatcher, true, "-std=c++11");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult notMatchesWithCuda(const Twine &Code,
|
|
|
|
const T &AMatcher) {
|
2014-08-05 17:45:53 +08:00
|
|
|
return matchesConditionallyWithCuda(Code, AMatcher, false, "-std=c++11");
|
|
|
|
}
|
|
|
|
|
2019-03-21 23:33:10 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchesWithOpenMP(const Twine &Code,
|
2019-03-21 23:33:10 +08:00
|
|
|
const T &AMatcher) {
|
2019-03-22 03:13:22 +08:00
|
|
|
return matchesConditionally(Code, AMatcher, true, "-fopenmp=libomp");
|
2019-03-21 23:33:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult notMatchesWithOpenMP(const Twine &Code,
|
2019-03-21 23:33:10 +08:00
|
|
|
const T &AMatcher) {
|
2019-03-22 03:13:22 +08:00
|
|
|
return matchesConditionally(Code, AMatcher, false, "-fopenmp=libomp");
|
2019-03-21 23:33:10 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchAndVerifyResultConditionally(
|
|
|
|
const Twine &Code, const T &AMatcher,
|
|
|
|
std::unique_ptr<BoundNodesCallback> FindResultVerifier, bool ExpectResult) {
|
2012-07-06 13:48:52 +08:00
|
|
|
bool VerifiedResult = false;
|
|
|
|
MatchFinder Finder;
|
2016-04-21 21:51:07 +08:00
|
|
|
VerifyMatch VerifyVerifiedResult(std::move(FindResultVerifier), &VerifiedResult);
|
2014-04-24 11:30:22 +08:00
|
|
|
Finder.addMatcher(AMatcher, &VerifyVerifiedResult);
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<FrontendActionFactory> Factory(
|
|
|
|
newFrontendActionFactory(&Finder));
|
2016-07-01 02:12:25 +08:00
|
|
|
// Some tests use typeof, which is a gnu extension. Using an explicit
|
|
|
|
// unknown-unknown triple is good for a large speedup, because it lets us
|
|
|
|
// avoid constructing a full system triple.
|
|
|
|
std::vector<std::string> Args = {"-std=gnu++98", "-target",
|
|
|
|
"i386-unknown-unknown"};
|
2012-08-30 10:08:31 +08:00
|
|
|
if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
|
|
|
|
}
|
|
|
|
if (!VerifiedResult && ExpectResult) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Could not verify result in \"" << Code << "\"";
|
|
|
|
} else if (VerifiedResult && !ExpectResult) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Verified unexpected result in \"" << Code << "\"";
|
|
|
|
}
|
2013-11-08 06:30:36 +08:00
|
|
|
|
|
|
|
VerifiedResult = false;
|
2020-06-03 04:20:58 +08:00
|
|
|
SmallString<256> Buffer;
|
|
|
|
std::unique_ptr<ASTUnit> AST(
|
|
|
|
buildASTFromCodeWithArgs(Code.toStringRef(Buffer), Args));
|
2013-11-08 06:30:36 +08:00
|
|
|
if (!AST.get())
|
|
|
|
return testing::AssertionFailure() << "Parsing error in \"" << Code
|
|
|
|
<< "\" while building AST";
|
|
|
|
Finder.matchAST(AST->getASTContext());
|
|
|
|
if (!VerifiedResult && ExpectResult) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Could not verify result in \"" << Code << "\" with AST";
|
|
|
|
} else if (VerifiedResult && !ExpectResult) {
|
|
|
|
return testing::AssertionFailure()
|
|
|
|
<< "Verified unexpected result in \"" << Code << "\" with AST";
|
|
|
|
}
|
|
|
|
|
2012-07-06 13:48:52 +08:00
|
|
|
return testing::AssertionSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Find better names for these functions (or document what they
|
|
|
|
// do more precisely).
|
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchAndVerifyResultTrue(
|
|
|
|
const Twine &Code, const T &AMatcher,
|
|
|
|
std::unique_ptr<BoundNodesCallback> FindResultVerifier) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return matchAndVerifyResultConditionally(
|
2016-04-21 21:51:07 +08:00
|
|
|
Code, AMatcher, std::move(FindResultVerifier), true);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-06-03 04:20:58 +08:00
|
|
|
testing::AssertionResult matchAndVerifyResultFalse(
|
|
|
|
const Twine &Code, const T &AMatcher,
|
|
|
|
std::unique_ptr<BoundNodesCallback> FindResultVerifier) {
|
2012-07-06 13:48:52 +08:00
|
|
|
return matchAndVerifyResultConditionally(
|
2016-04-21 21:51:07 +08:00
|
|
|
Code, AMatcher, std::move(FindResultVerifier), false);
|
2012-07-06 13:48:52 +08:00
|
|
|
}
|
|
|
|
|
2016-05-18 03:22:57 +08:00
|
|
|
// Implements a run method that returns whether BoundNodes contains a
|
|
|
|
// Decl bound to Id that can be dynamically cast to T.
|
|
|
|
// Optionally checks that the check succeeded a specific number of times.
|
|
|
|
template <typename T>
|
|
|
|
class VerifyIdIsBoundTo : public BoundNodesCallback {
|
|
|
|
public:
|
|
|
|
// Create an object that checks that a node of type \c T was bound to \c Id.
|
|
|
|
// Does not check for a certain number of matches.
|
|
|
|
explicit VerifyIdIsBoundTo(llvm::StringRef Id)
|
2020-01-29 03:23:46 +08:00
|
|
|
: Id(std::string(Id)), ExpectedCount(-1), Count(0) {}
|
2016-05-18 03:22:57 +08:00
|
|
|
|
|
|
|
// Create an object that checks that a node of type \c T was bound to \c Id.
|
|
|
|
// Checks that there were exactly \c ExpectedCount matches.
|
|
|
|
VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount)
|
2020-01-29 03:23:46 +08:00
|
|
|
: Id(std::string(Id)), ExpectedCount(ExpectedCount), Count(0) {}
|
2016-05-18 03:22:57 +08:00
|
|
|
|
|
|
|
// Create an object that checks that a node of type \c T was bound to \c Id.
|
|
|
|
// Checks that there was exactly one match with the name \c ExpectedName.
|
|
|
|
// Note that \c T must be a NamedDecl for this to work.
|
|
|
|
VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName,
|
|
|
|
int ExpectedCount = 1)
|
2020-01-29 03:23:46 +08:00
|
|
|
: Id(std::string(Id)), ExpectedCount(ExpectedCount), Count(0),
|
|
|
|
ExpectedName(std::string(ExpectedName)) {}
|
2016-05-18 03:22:57 +08:00
|
|
|
|
|
|
|
void onEndOfTranslationUnit() override {
|
2017-06-16 05:01:24 +08:00
|
|
|
if (ExpectedCount != -1) {
|
2016-05-18 03:22:57 +08:00
|
|
|
EXPECT_EQ(ExpectedCount, Count);
|
2017-06-16 05:01:24 +08:00
|
|
|
}
|
|
|
|
if (!ExpectedName.empty()) {
|
2016-05-18 03:22:57 +08:00
|
|
|
EXPECT_EQ(ExpectedName, Name);
|
2017-06-16 05:01:24 +08:00
|
|
|
}
|
2016-05-18 03:22:57 +08:00
|
|
|
Count = 0;
|
|
|
|
Name.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~VerifyIdIsBoundTo() override {
|
|
|
|
EXPECT_EQ(0, Count);
|
|
|
|
EXPECT_EQ("", Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool run(const BoundNodes *Nodes) override {
|
|
|
|
const BoundNodes::IDToNodeMap &M = Nodes->getMap();
|
|
|
|
if (Nodes->getNodeAs<T>(Id)) {
|
|
|
|
++Count;
|
|
|
|
if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) {
|
|
|
|
Name = Named->getNameAsString();
|
|
|
|
} else if (const NestedNameSpecifier *NNS =
|
|
|
|
Nodes->getNodeAs<NestedNameSpecifier>(Id)) {
|
|
|
|
llvm::raw_string_ostream OS(Name);
|
|
|
|
NNS->print(OS, PrintingPolicy(LangOptions()));
|
|
|
|
}
|
|
|
|
BoundNodes::IDToNodeMap::const_iterator I = M.find(Id);
|
|
|
|
EXPECT_NE(M.end(), I);
|
2017-06-16 05:01:24 +08:00
|
|
|
if (I != M.end()) {
|
2016-05-18 03:22:57 +08:00
|
|
|
EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>());
|
2017-06-16 05:01:24 +08:00
|
|
|
}
|
2016-05-18 03:22:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(M.count(Id) == 0 ||
|
|
|
|
M.find(Id)->second.template get<T>() == nullptr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool run(const BoundNodes *Nodes, ASTContext *Context) override {
|
|
|
|
return run(Nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string Id;
|
|
|
|
const int ExpectedCount;
|
|
|
|
int Count;
|
|
|
|
const std::string ExpectedName;
|
|
|
|
std::string Name;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ast_matchers
|
|
|
|
} // namespace clang
|
2012-07-06 13:48:52 +08:00
|
|
|
|
|
|
|
#endif // LLVM_CLANG_UNITTESTS_AST_MATCHERS_AST_MATCHERS_TEST_H
|