ASTUnitTest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit tests -----------------===//
  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. #include <fstream>
  9. #include "clang/Frontend/ASTUnit.h"
  10. #include "clang/Frontend/CompilerInstance.h"
  11. #include "clang/Frontend/CompilerInvocation.h"
  12. #include "clang/Frontend/PCHContainerOperations.h"
  13. #include "llvm/Support/FileSystem.h"
  14. #include "llvm/Support/Path.h"
  15. #include "llvm/Support/ToolOutputFile.h"
  16. #include "gtest/gtest.h"
  17. using namespace llvm;
  18. using namespace clang;
  19. namespace {
  20. class ASTUnitTest : public ::testing::Test {
  21. protected:
  22. int FD;
  23. llvm::SmallString<256> InputFileName;
  24. std::unique_ptr<ToolOutputFile> input_file;
  25. IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
  26. std::shared_ptr<CompilerInvocation> CInvok;
  27. std::shared_ptr<PCHContainerOperations> PCHContainerOps;
  28. std::unique_ptr<ASTUnit> createASTUnit(bool isVolatile) {
  29. EXPECT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD,
  30. InputFileName));
  31. input_file = std::make_unique<ToolOutputFile>(InputFileName, FD);
  32. input_file->os() << "";
  33. const char *Args[] = {"clang", "-xc++", InputFileName.c_str()};
  34. Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
  35. CInvok = createInvocationFromCommandLine(Args, Diags);
  36. if (!CInvok)
  37. return nullptr;
  38. FileManager *FileMgr =
  39. new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
  40. PCHContainerOps = std::make_shared<PCHContainerOperations>();
  41. return ASTUnit::LoadFromCompilerInvocation(
  42. CInvok, PCHContainerOps, Diags, FileMgr, false, CaptureDiagsKind::None,
  43. 0, TU_Complete, false, false, isVolatile);
  44. }
  45. };
  46. TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) {
  47. // Check that the printing policy is restored with the correct language
  48. // options when loading an ASTUnit from a file. To this end, an ASTUnit
  49. // for a C++ translation unit is set up and written to a temporary file.
  50. // By default `UseVoidForZeroParams` is true for non-C++ language options,
  51. // thus we can check this field after loading the ASTUnit to deduce whether
  52. // the correct (C++) language options were used when setting up the printing
  53. // policy.
  54. {
  55. PrintingPolicy PolicyWithDefaultLangOpt(LangOptions{});
  56. EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams);
  57. }
  58. std::unique_ptr<ASTUnit> AST = createASTUnit(false);
  59. if (!AST)
  60. FAIL() << "failed to create ASTUnit";
  61. EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
  62. llvm::SmallString<256> ASTFileName;
  63. ASSERT_FALSE(
  64. llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName));
  65. ToolOutputFile ast_file(ASTFileName, FD);
  66. AST->Save(ASTFileName.str());
  67. EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName));
  68. std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
  69. ASTFileName.str(), PCHContainerOps->getRawReader(),
  70. ASTUnit::LoadEverything, Diags, FileSystemOptions(),
  71. /*UseDebugInfo=*/false);
  72. if (!AU)
  73. FAIL() << "failed to load ASTUnit";
  74. EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
  75. }
  76. TEST_F(ASTUnitTest, GetBufferForFileMemoryMapping) {
  77. std::unique_ptr<ASTUnit> AST = createASTUnit(true);
  78. if (!AST)
  79. FAIL() << "failed to create ASTUnit";
  80. std::unique_ptr<llvm::MemoryBuffer> memoryBuffer =
  81. AST->getBufferForFile(InputFileName);
  82. EXPECT_NE(memoryBuffer->getBufferKind(),
  83. llvm::MemoryBuffer::MemoryBuffer_MMap);
  84. }
  85. } // anonymous namespace