| 1 |
//===- unittest/AST/ASTImporterFixtures.h - AST unit test support ---------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// Fixture classes for testing the ASTImporter. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#ifndef LLVM_CLANG_UNITTESTS_AST_IMPORTER_FIXTURES_H |
| 15 |
#define LLVM_CLANG_UNITTESTS_AST_IMPORTER_FIXTURES_H |
| 16 |
|
| 17 |
#include "gmock/gmock.h" |
| 18 |
|
| 19 |
#include "clang/AST/ASTImporter.h" |
| 20 |
#include "clang/AST/ASTImporterSharedState.h" |
| 21 |
#include "clang/Frontend/ASTUnit.h" |
| 22 |
#include "clang/Testing/CommandLineArgs.h" |
| 23 |
#include "llvm/Support/Error.h" |
| 24 |
#include "llvm/Support/ErrorHandling.h" |
| 25 |
|
| 26 |
#include "DeclMatcher.h" |
| 27 |
#include "MatchVerifier.h" |
| 28 |
|
| 29 |
#include <sstream> |
| 30 |
|
| 31 |
namespace clang { |
| 32 |
|
| 33 |
class ASTImporter; |
| 34 |
class ASTImporterSharedState; |
| 35 |
class ASTUnit; |
| 36 |
|
| 37 |
namespace ast_matchers { |
| 38 |
|
| 39 |
const StringRef DeclToImportID = "declToImport"; |
| 40 |
const StringRef DeclToVerifyID = "declToVerify"; |
| 41 |
|
| 42 |
// Creates a virtual file and assigns that to the context of given AST. If the |
| 43 |
// file already exists then the file will not be created again as a duplicate. |
| 44 |
void createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName, |
| 45 |
std::unique_ptr<llvm::MemoryBuffer> &&Buffer); |
| 46 |
|
| 47 |
void createVirtualFileIfNeeded(ASTUnit *ToAST, StringRef FileName, |
| 48 |
StringRef Code); |
| 49 |
|
| 50 |
// Common base for the different families of ASTImporter tests that are |
| 51 |
// parameterized on the compiler options which may result a different AST. E.g. |
| 52 |
// -fms-compatibility or -fdelayed-template-parsing. |
| 53 |
class CompilerOptionSpecificTest : public ::testing::Test { |
| 54 |
protected: |
| 55 |
// Return the extra arguments appended to runtime options at compilation. |
| 56 |
virtual std::vector<std::string> getExtraArgs() const { return {}; } |
| 57 |
|
| 58 |
// Returns the argument vector used for a specific language option, this set |
| 59 |
// can be tweaked by the test parameters. |
| 60 |
std::vector<std::string> |
| 61 |
getCommandLineArgsForLanguage(TestLanguage Lang) const { |
| 62 |
std::vector<std::string> Args = getCommandLineArgsForTesting(Lang); |
| 63 |
std::vector<std::string> ExtraArgs = getExtraArgs(); |
| 64 |
for (const auto &Arg : ExtraArgs) { |
| 65 |
Args.push_back(Arg); |
| 66 |
} |
| 67 |
return Args; |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
const auto DefaultTestArrayForRunOptions = |
| 72 |
std::array<std::vector<std::string>, 4>{ |
| 73 |
{std::vector<std::string>(), |
| 74 |
std::vector<std::string>{"-fdelayed-template-parsing"}, |
| 75 |
std::vector<std::string>{"-fms-compatibility"}, |
| 76 |
std::vector<std::string>{"-fdelayed-template-parsing", |
| 77 |
"-fms-compatibility"}}}; |
| 78 |
|
| 79 |
const auto DefaultTestValuesForRunOptions = |
| 80 |
::testing::ValuesIn(DefaultTestArrayForRunOptions); |
| 81 |
|
| 82 |
// This class provides generic methods to write tests which can check internal |
| 83 |
// attributes of AST nodes like getPreviousDecl(), isVirtual(), etc. Also, |
| 84 |
// this fixture makes it possible to import from several "From" contexts. |
| 85 |
class ASTImporterTestBase : public CompilerOptionSpecificTest { |
| 86 |
|
| 87 |
const char *const InputFileName = "input.cc"; |
| 88 |
const char *const OutputFileName = "output.cc"; |
| 89 |
|
| 90 |
public: |
| 91 |
/// Allocates an ASTImporter (or one of its subclasses). |
| 92 |
typedef std::function<ASTImporter *( |
| 93 |
ASTContext &, FileManager &, ASTContext &, FileManager &, bool, |
| 94 |
const std::shared_ptr<ASTImporterSharedState> &SharedState)> |
| 95 |
ImporterConstructor; |
| 96 |
|
| 97 |
// ODR handling type for the AST importer. |
| 98 |
ASTImporter::ODRHandlingType ODRHandling; |
| 99 |
|
| 100 |
// The lambda that constructs the ASTImporter we use in this test. |
| 101 |
ImporterConstructor Creator; |
| 102 |
|
| 103 |
private: |
| 104 |
// Buffer for the To context, must live in the test scope. |
| 105 |
std::string ToCode; |
| 106 |
|
| 107 |
// Represents a "From" translation unit and holds an importer object which we |
| 108 |
// use to import from this translation unit. |
| 109 |
struct TU { |
| 110 |
// Buffer for the context, must live in the test scope. |
| 111 |
std::string Code; |
| 112 |
std::string FileName; |
| 113 |
std::unique_ptr<ASTUnit> Unit; |
| 114 |
TranslationUnitDecl *TUDecl = nullptr; |
| 115 |
std::unique_ptr<ASTImporter> Importer; |
| 116 |
ImporterConstructor Creator; |
| 117 |
ASTImporter::ODRHandlingType ODRHandling; |
| 118 |
|
| 119 |
TU(StringRef Code, StringRef FileName, std::vector<std::string> Args, |
| 120 |
ImporterConstructor C = ImporterConstructor(), |
| 121 |
ASTImporter::ODRHandlingType ODRHandling = |
| 122 |
ASTImporter::ODRHandlingType::Conservative); |
| 123 |
~TU(); |
| 124 |
|
| 125 |
void |
| 126 |
lazyInitImporter(const std::shared_ptr<ASTImporterSharedState> &SharedState, |
| 127 |
ASTUnit *ToAST); |
| 128 |
Decl *import(const std::shared_ptr<ASTImporterSharedState> &SharedState, |
| 129 |
ASTUnit *ToAST, Decl *FromDecl); |
| 130 |
llvm::Expected<Decl *> |
| 131 |
importOrError(const std::shared_ptr<ASTImporterSharedState> &SharedState, |
| 132 |
ASTUnit *ToAST, Decl *FromDecl); |
| 133 |
QualType import(const std::shared_ptr<ASTImporterSharedState> &SharedState, |
| 134 |
ASTUnit *ToAST, QualType FromType); |
| 135 |
}; |
| 136 |
|
| 137 |
// We may have several From contexts and related translation units. In each |
| 138 |
// AST, the buffers for the source are handled via references and are set |
| 139 |
// during the creation of the AST. These references must point to a valid |
| 140 |
// buffer until the AST is alive. Thus, we must use a list in order to avoid |
| 141 |
// moving of the stored objects because that would mean breaking the |
| 142 |
// references in the AST. By using a vector a move could happen when the |
| 143 |
// vector is expanding, with the list we won't have these issues. |
| 144 |
std::list<TU> FromTUs; |
| 145 |
|
| 146 |
// Initialize the shared state if not initialized already. |
| 147 |
void lazyInitSharedState(TranslationUnitDecl *ToTU); |
| 148 |
|
| 149 |
void lazyInitToAST(TestLanguage ToLang, StringRef ToSrcCode, |
| 150 |
StringRef FileName); |
| 151 |
|
| 152 |
protected: |
| 153 |
std::shared_ptr<ASTImporterSharedState> SharedStatePtr; |
| 154 |
|
| 155 |
public: |
| 156 |
// We may have several From context but only one To context. |
| 157 |
std::unique_ptr<ASTUnit> ToAST; |
| 158 |
|
| 159 |
// Returns with the TU associated with the given Decl. |
| 160 |
TU *findFromTU(Decl *From); |
| 161 |
|
| 162 |
// Creates an AST both for the From and To source code and imports the Decl |
| 163 |
// of the identifier into the To context. |
| 164 |
// Must not be called more than once within the same test. |
| 165 |
std::tuple<Decl *, Decl *> |
| 166 |
getImportedDecl(StringRef FromSrcCode, TestLanguage FromLang, |
| 167 |
StringRef ToSrcCode, TestLanguage ToLang, |
| 168 |
StringRef Identifier = DeclToImportID); |
| 169 |
|
| 170 |
// Creates a TU decl for the given source code which can be used as a From |
| 171 |
// context. May be called several times in a given test (with different file |
| 172 |
// name). |
| 173 |
TranslationUnitDecl *getTuDecl(StringRef SrcCode, TestLanguage Lang, |
| 174 |
StringRef FileName = "input.cc"); |
| 175 |
|
| 176 |
// Creates the To context with the given source code and returns the TU decl. |
| 177 |
TranslationUnitDecl *getToTuDecl(StringRef ToSrcCode, TestLanguage ToLang); |
| 178 |
|
| 179 |
// Import the given Decl into the ToCtx. |
| 180 |
// May be called several times in a given test. |
| 181 |
// The different instances of the param From may have different ASTContext. |
| 182 |
Decl *Import(Decl *From, TestLanguage ToLang); |
| 183 |
|
| 184 |
template <class DeclT> DeclT *Import(DeclT *From, TestLanguage Lang) { |
| 185 |
return cast_or_null<DeclT>(Import(cast<Decl>(From), Lang)); |
| 186 |
} |
| 187 |
|
| 188 |
// Import the given Decl into the ToCtx. |
| 189 |
// Same as Import but returns the result of the import which can be an error. |
| 190 |
llvm::Expected<Decl *> importOrError(Decl *From, TestLanguage ToLang); |
| 191 |
|
| 192 |
QualType ImportType(QualType FromType, Decl *TUDecl, TestLanguage ToLang); |
| 193 |
|
| 194 |
ASTImporterTestBase() |
| 195 |
: ODRHandling(ASTImporter::ODRHandlingType::Conservative) {} |
| 196 |
~ASTImporterTestBase(); |
| 197 |
}; |
| 198 |
|
| 199 |
class ASTImporterOptionSpecificTestBase |
| 200 |
: public ASTImporterTestBase, |
| 201 |
public ::testing::WithParamInterface<std::vector<std::string>> { |
| 202 |
protected: |
| 203 |
std::vector<std::string> getExtraArgs() const override { return GetParam(); } |
| 204 |
}; |
| 205 |
|
| 206 |
// Base class for those tests which use the family of `testImport` functions. |
| 207 |
class TestImportBase |
| 208 |
: public CompilerOptionSpecificTest, |
| 209 |
public ::testing::WithParamInterface<std::vector<std::string>> { |
| 210 |
|
| 211 |
template <typename NodeType> |
| 212 |
llvm::Expected<NodeType> importNode(ASTUnit *From, ASTUnit *To, |
| 213 |
ASTImporter &Importer, NodeType Node) { |
| 214 |
ASTContext &ToCtx = To->getASTContext(); |
| 215 |
|
| 216 |
// Add 'From' file to virtual file system so importer can 'find' it |
| 217 |
// while importing SourceLocations. It is safe to add same file multiple |
| 218 |
// times - it just isn't replaced. |
| 219 |
StringRef FromFileName = From->getMainFileName(); |
| 220 |
createVirtualFileIfNeeded(To, FromFileName, |
| 221 |
From->getBufferForFile(FromFileName)); |
| 222 |
|
| 223 |
auto Imported = Importer.Import(Node); |
| 224 |
|
| 225 |
if (Imported) { |
| 226 |
// This should dump source locations and assert if some source locations |
| 227 |
// were not imported. |
| 228 |
SmallString<1024> ImportChecker; |
| 229 |
llvm::raw_svector_ostream ToNothing(ImportChecker); |
| 230 |
ToCtx.getTranslationUnitDecl()->print(ToNothing); |
| 231 |
|
| 232 |
// This traverses the AST to catch certain bugs like poorly or not |
| 233 |
// implemented subtrees. |
| 234 |
(*Imported)->dump(ToNothing); |
| 235 |
} |
| 236 |
|
| 237 |
return Imported; |
| 238 |
} |
| 239 |
|
| 240 |
template <typename NodeType> |
| 241 |
testing::AssertionResult |
| 242 |
testImport(const std::string &FromCode, |
| 243 |
const std::vector<std::string> &FromArgs, |
| 244 |
const std::string &ToCode, const std::vector<std::string> &ToArgs, |
| 245 |
MatchVerifier<NodeType> &Verifier, |
| 246 |
const internal::BindableMatcher<NodeType> &SearchMatcher, |
| 247 |
const internal::BindableMatcher<NodeType> &VerificationMatcher) { |
| 248 |
const char *const InputFileName = "input.cc"; |
| 249 |
const char *const OutputFileName = "output.cc"; |
| 250 |
|
| 251 |
std::unique_ptr<ASTUnit> FromAST = tooling::buildASTFromCodeWithArgs( |
| 252 |
FromCode, FromArgs, InputFileName), |
| 253 |
ToAST = tooling::buildASTFromCodeWithArgs( |
| 254 |
ToCode, ToArgs, OutputFileName); |
| 255 |
|
| 256 |
ASTContext &FromCtx = FromAST->getASTContext(), |
| 257 |
&ToCtx = ToAST->getASTContext(); |
| 258 |
|
| 259 |
ASTImporter Importer(ToCtx, ToAST->getFileManager(), FromCtx, |
| 260 |
FromAST->getFileManager(), false); |
| 261 |
|
| 262 |
auto FoundNodes = match(SearchMatcher, FromCtx); |
| 263 |
if (FoundNodes.size() != 1) |
| 264 |
return testing::AssertionFailure() |
| 265 |
<< "Multiple potential nodes were found!"; |
| 266 |
|
| 267 |
auto ToImport = selectFirst<NodeType>(DeclToImportID, FoundNodes); |
| 268 |
if (!ToImport) |
| 269 |
return testing::AssertionFailure() << "Node type mismatch!"; |
| 270 |
|
| 271 |
// The node being imported should match in the same way as |
| 272 |
// the result node. |
| 273 |
internal::BindableMatcher<NodeType> WrapperMatcher(VerificationMatcher); |
| 274 |
EXPECT_TRUE(Verifier.match(ToImport, WrapperMatcher)); |
| 275 |
|
| 276 |
auto Imported = importNode(FromAST.get(), ToAST.get(), Importer, ToImport); |
| 277 |
if (!Imported) { |
| 278 |
std::string ErrorText; |
| 279 |
handleAllErrors(Imported.takeError(), |
| 280 |
[&ErrorText](const ASTImportError &Err) { |
| 281 |
ErrorText = Err.message(); |
| 282 |
}); |
| 283 |
return testing::AssertionFailure() |
| 284 |
<< "Import failed, error: \"" << ErrorText << "\"!"; |
| 285 |
} |
| 286 |
|
| 287 |
return Verifier.match(*Imported, WrapperMatcher); |
| 288 |
} |
| 289 |
|
| 290 |
template <typename NodeType> |
| 291 |
testing::AssertionResult |
| 292 |
testImport(const std::string &FromCode, |
| 293 |
const std::vector<std::string> &FromArgs, |
| 294 |
const std::string &ToCode, const std::vector<std::string> &ToArgs, |
| 295 |
MatchVerifier<NodeType> &Verifier, |
| 296 |
const internal::BindableMatcher<NodeType> &VerificationMatcher) { |
| 297 |
return testImport( |
| 298 |
FromCode, FromArgs, ToCode, ToArgs, Verifier, |
| 299 |
translationUnitDecl( |
| 300 |
has(namedDecl(hasName(DeclToImportID)).bind(DeclToImportID))), |
| 301 |
VerificationMatcher); |
| 302 |
} |
| 303 |
|
| 304 |
protected: |
| 305 |
std::vector<std::string> getExtraArgs() const override { return GetParam(); } |
| 306 |
|
| 307 |
public: |
| 308 |
/// Test how AST node named "declToImport" located in the translation unit |
| 309 |
/// of "FromCode" virtual file is imported to "ToCode" virtual file. |
| 310 |
/// The verification is done by running AMatcher over the imported node. |
| 311 |
template <typename NodeType, typename MatcherType> |
| 312 |
void testImport(const std::string &FromCode, TestLanguage FromLang, |
| 313 |
const std::string &ToCode, TestLanguage ToLang, |
| 314 |
MatchVerifier<NodeType> &Verifier, |
| 315 |
const MatcherType &AMatcher) { |
| 316 |
std::vector<std::string> FromArgs = getCommandLineArgsForLanguage(FromLang); |
| 317 |
std::vector<std::string> ToArgs = getCommandLineArgsForLanguage(ToLang); |
| 318 |
EXPECT_TRUE( |
| 319 |
testImport(FromCode, FromArgs, ToCode, ToArgs, Verifier, AMatcher)); |
| 320 |
} |
| 321 |
|
| 322 |
struct ImportAction { |
| 323 |
StringRef FromFilename; |
| 324 |
StringRef ToFilename; |
| 325 |
// FIXME: Generalize this to support other node kinds. |
| 326 |
internal::BindableMatcher<Decl> ImportPredicate; |
| 327 |
|
| 328 |
ImportAction(StringRef FromFilename, StringRef ToFilename, |
| 329 |
DeclarationMatcher ImportPredicate) |
| 330 |
: FromFilename(FromFilename), ToFilename(ToFilename), |
| 331 |
ImportPredicate(ImportPredicate) {} |
| 332 |
|
| 333 |
ImportAction(StringRef FromFilename, StringRef ToFilename, |
| 334 |
const std::string &DeclName) |
| 335 |
: FromFilename(FromFilename), ToFilename(ToFilename), |
| 336 |
ImportPredicate(namedDecl(hasName(DeclName))) {} |
| 337 |
}; |
| 338 |
|
| 339 |
using SingleASTUnit = std::unique_ptr<ASTUnit>; |
| 340 |
using AllASTUnits = llvm::StringMap<SingleASTUnit>; |
| 341 |
|
| 342 |
struct CodeEntry { |
| 343 |
std::string CodeSample; |
| 344 |
TestLanguage Lang; |
| 345 |
}; |
| 346 |
|
| 347 |
using CodeFiles = llvm::StringMap<CodeEntry>; |
| 348 |
|
| 349 |
/// Builds an ASTUnit for one potential compile options set. |
| 350 |
SingleASTUnit createASTUnit(StringRef FileName, const CodeEntry &CE) const { |
| 351 |
std::vector<std::string> Args = getCommandLineArgsForLanguage(CE.Lang); |
| 352 |
auto AST = tooling::buildASTFromCodeWithArgs(CE.CodeSample, Args, FileName); |
| 353 |
EXPECT_TRUE(AST.get()); |
| 354 |
return AST; |
| 355 |
} |
| 356 |
|
| 357 |
/// Test an arbitrary sequence of imports for a set of given in-memory files. |
| 358 |
/// The verification is done by running VerificationMatcher against a |
| 359 |
/// specified AST node inside of one of given files. |
| 360 |
/// \param CodeSamples Map whose key is the file name and the value is the |
| 361 |
/// file content. |
| 362 |
/// \param ImportActions Sequence of imports. Each import in sequence |
| 363 |
/// specifies "from file" and "to file" and a matcher that is used for |
| 364 |
/// searching a declaration for import in "from file". |
| 365 |
/// \param FileForFinalCheck Name of virtual file for which the final check is |
| 366 |
/// applied. |
| 367 |
/// \param FinalSelectPredicate Matcher that specifies the AST node in the |
| 368 |
/// FileForFinalCheck for which the verification will be done. |
| 369 |
/// \param VerificationMatcher Matcher that will be used for verification |
| 370 |
/// after all imports in sequence are done. |
| 371 |
void testImportSequence(const CodeFiles &CodeSamples, |
| 372 |
const std::vector<ImportAction> &ImportActions, |
| 373 |
StringRef FileForFinalCheck, |
| 374 |
internal::BindableMatcher<Decl> FinalSelectPredicate, |
| 375 |
internal::BindableMatcher<Decl> VerificationMatcher) { |
| 376 |
AllASTUnits AllASTs; |
| 377 |
using ImporterKey = std::pair<const ASTUnit *, const ASTUnit *>; |
| 378 |
llvm::DenseMap<ImporterKey, std::unique_ptr<ASTImporter>> Importers; |
| 379 |
|
| 380 |
auto GenASTsIfNeeded = [this, &AllASTs, &CodeSamples](StringRef Filename) { |
| 381 |
if (!AllASTs.count(Filename)) { |
| 382 |
auto Found = CodeSamples.find(Filename); |
| 383 |
assert(Found != CodeSamples.end() && "Wrong file for import!"); |
| 384 |
AllASTs[Filename] = createASTUnit(Filename, Found->getValue()); |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
for (const ImportAction &Action : ImportActions) { |
| 389 |
StringRef FromFile = Action.FromFilename, ToFile = Action.ToFilename; |
| 390 |
GenASTsIfNeeded(FromFile); |
| 391 |
GenASTsIfNeeded(ToFile); |
| 392 |
|
| 393 |
ASTUnit *From = AllASTs[FromFile].get(); |
| 394 |
ASTUnit *To = AllASTs[ToFile].get(); |
| 395 |
|
| 396 |
// Create a new importer if needed. |
| 397 |
std::unique_ptr<ASTImporter> &ImporterRef = Importers[{From, To}]; |
| 398 |
if (!ImporterRef) |
| 399 |
ImporterRef.reset(new ASTImporter( |
| 400 |
To->getASTContext(), To->getFileManager(), From->getASTContext(), |
| 401 |
From->getFileManager(), false)); |
| 402 |
|
| 403 |
// Find the declaration and import it. |
| 404 |
auto FoundDecl = match(Action.ImportPredicate.bind(DeclToImportID), |
| 405 |
From->getASTContext()); |
| 406 |
EXPECT_TRUE(FoundDecl.size() == 1); |
| 407 |
const Decl *ToImport = selectFirst<Decl>(DeclToImportID, FoundDecl); |
| 408 |
auto Imported = importNode(From, To, *ImporterRef, ToImport); |
| 409 |
EXPECT_TRUE(static_cast<bool>(Imported)); |
| 410 |
if (!Imported) |
| 411 |
llvm::consumeError(Imported.takeError()); |
| 412 |
} |
| 413 |
|
| 414 |
// Find the declaration and import it. |
| 415 |
auto FoundDecl = match(FinalSelectPredicate.bind(DeclToVerifyID), |
| 416 |
AllASTs[FileForFinalCheck]->getASTContext()); |
| 417 |
EXPECT_TRUE(FoundDecl.size() == 1); |
| 418 |
const Decl *ToVerify = selectFirst<Decl>(DeclToVerifyID, FoundDecl); |
| 419 |
MatchVerifier<Decl> Verifier; |
| 420 |
EXPECT_TRUE(Verifier.match( |
| 421 |
ToVerify, internal::BindableMatcher<Decl>(VerificationMatcher))); |
| 422 |
} |
| 423 |
}; |
| 424 |
|
| 425 |
template <typename T> RecordDecl *getRecordDecl(T *D) { |
| 426 |
auto *ET = cast<ElaboratedType>(D->getType().getTypePtr()); |
| 427 |
return cast<RecordType>(ET->getNamedType().getTypePtr())->getDecl(); |
| 428 |
} |
| 429 |
|
| 430 |
template <class T> |
| 431 |
::testing::AssertionResult isSuccess(llvm::Expected<T> &ValOrErr) { |
| 432 |
if (ValOrErr) |
| 433 |
return ::testing::AssertionSuccess() << "Expected<> contains no error."; |
| 434 |
else |
| 435 |
return ::testing::AssertionFailure() |
| 436 |
<< "Expected<> contains error: " << toString(ValOrErr.takeError()); |
| 437 |
} |
| 438 |
|
| 439 |
template <class T> |
| 440 |
::testing::AssertionResult isImportError(llvm::Expected<T> &ValOrErr, |
| 441 |
ASTImportError::ErrorKind Kind) { |
| 442 |
if (ValOrErr) { |
| 443 |
return ::testing::AssertionFailure() << "Expected<> is expected to contain " |
| 444 |
"error but does contain value \"" |
| 445 |
<< (*ValOrErr) << "\""; |
| 446 |
} else { |
| 447 |
std::ostringstream OS; |
| 448 |
bool Result = false; |
| 449 |
auto Err = llvm::handleErrors( |
| 450 |
ValOrErr.takeError(), [&OS, &Result, Kind](clang::ASTImportError &IE) { |
| 451 |
if (IE.Error == Kind) { |
| 452 |
Result = true; |
| 453 |
OS << "Expected<> contains an ImportError " << IE.toString(); |
| 454 |
} else { |
| 455 |
OS << "Expected<> contains an ImportError " << IE.toString() |
| 456 |
<< " instead of kind " << Kind; |
| 457 |
} |
| 458 |
}); |
| 459 |
if (Err) { |
| 460 |
OS << "Expected<> contains unexpected error: " |
| 461 |
<< toString(std::move(Err)); |
| 462 |
} |
| 463 |
if (Result) |
| 464 |
return ::testing::AssertionSuccess() << OS.str(); |
| 465 |
else |
| 466 |
return ::testing::AssertionFailure() << OS.str(); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
} // end namespace ast_matchers |
| 471 |
} // end namespace clang |
| 472 |
|
| 473 |
#endif |