ASTUnitTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit tests -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include <fstream>
  10. #include "clang/Frontend/ASTUnit.h"
  11. #include "clang/Frontend/CompilerInstance.h"
  12. #include "clang/Frontend/CompilerInvocation.h"
  13. #include "clang/Frontend/PCHContainerOperations.h"
  14. #include "llvm/Support/FileSystem.h"
  15. #include "llvm/Support/Path.h"
  16. #include "llvm/Support/ToolOutputFile.h"
  17. #include "gtest/gtest.h"
  18. using namespace llvm;
  19. using namespace clang;
  20. namespace {
  21. TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
  22. // Check that the printing policy is restored with the correct language
  23. // options when loading an ASTUnit from a file. To this end, an ASTUnit
  24. // for a C++ translation unit is set up and written to a temporary file.
  25. // By default `UseVoidForZeroParams` is true for non-C++ language options,
  26. // thus we can check this field after loading the ASTUnit to deduce whether
  27. // the correct (C++) language options were used when setting up the printing
  28. // policy.
  29. {
  30. PrintingPolicy PolicyWithDefaultLangOpt(LangOptions{});
  31. EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams);
  32. }
  33. int FD;
  34. llvm::SmallString<256> InputFileName;
  35. ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD, InputFileName));
  36. ToolOutputFile input_file(InputFileName, FD);
  37. input_file.os() << "";
  38. const char* Args[] = {"clang", "-xc++", InputFileName.c_str()};
  39. IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
  40. CompilerInstance::createDiagnostics(new DiagnosticOptions());
  41. std::shared_ptr<CompilerInvocation> CInvok =
  42. createInvocationFromCommandLine(Args, Diags);
  43. if (!CInvok)
  44. FAIL() << "could not create compiler invocation";
  45. FileManager *FileMgr =
  46. new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
  47. auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
  48. std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
  49. CInvok, PCHContainerOps, Diags, FileMgr);
  50. if (!AST)
  51. FAIL() << "failed to create ASTUnit";
  52. EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
  53. llvm::SmallString<256> ASTFileName;
  54. ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName));
  55. ToolOutputFile ast_file(ASTFileName, FD);
  56. AST->Save(ASTFileName.str());
  57. EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName));
  58. std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
  59. ASTFileName.str(), PCHContainerOps->getRawReader(), ASTUnit::LoadEverything, Diags,
  60. FileSystemOptions(), /*UseDebugInfo=*/false);
  61. if (!AU)
  62. FAIL() << "failed to load ASTUnit";
  63. EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
  64. }
  65. } // anonymous namespace