PPConditionalDirectiveRecordTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive 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 "clang/Lex/PPConditionalDirectiveRecord.h"
  9. #include "clang/Basic/Diagnostic.h"
  10. #include "clang/Basic/DiagnosticOptions.h"
  11. #include "clang/Basic/FileManager.h"
  12. #include "clang/Basic/LangOptions.h"
  13. #include "clang/Basic/SourceManager.h"
  14. #include "clang/Basic/TargetInfo.h"
  15. #include "clang/Basic/TargetOptions.h"
  16. #include "clang/Lex/HeaderSearch.h"
  17. #include "clang/Lex/HeaderSearchOptions.h"
  18. #include "clang/Lex/ModuleLoader.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Lex/PreprocessorOptions.h"
  21. #include "gtest/gtest.h"
  22. using namespace clang;
  23. namespace {
  24. // The test fixture.
  25. class PPConditionalDirectiveRecordTest : public ::testing::Test {
  26. protected:
  27. PPConditionalDirectiveRecordTest()
  28. : FileMgr(FileMgrOpts),
  29. DiagID(new DiagnosticIDs()),
  30. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  31. SourceMgr(Diags, FileMgr),
  32. TargetOpts(new TargetOptions)
  33. {
  34. TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
  35. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  36. }
  37. FileSystemOptions FileMgrOpts;
  38. FileManager FileMgr;
  39. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  40. DiagnosticsEngine Diags;
  41. SourceManager SourceMgr;
  42. LangOptions LangOpts;
  43. std::shared_ptr<TargetOptions> TargetOpts;
  44. IntrusiveRefCntPtr<TargetInfo> Target;
  45. };
  46. TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
  47. const char *source =
  48. "0 1\n"
  49. "#if 1\n"
  50. "2\n"
  51. "#ifndef BB\n"
  52. "3 4\n"
  53. "#else\n"
  54. "#endif\n"
  55. "5\n"
  56. "#endif\n"
  57. "6\n"
  58. "#if 1\n"
  59. "7\n"
  60. "#if 1\n"
  61. "#endif\n"
  62. "8\n"
  63. "#endif\n"
  64. "9\n";
  65. std::unique_ptr<llvm::MemoryBuffer> Buf =
  66. llvm::MemoryBuffer::getMemBuffer(source);
  67. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
  68. TrivialModuleLoader ModLoader;
  69. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  70. Diags, LangOpts, Target.get());
  71. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  72. SourceMgr, HeaderInfo, ModLoader,
  73. /*IILookup =*/nullptr,
  74. /*OwnsHeaderSearch =*/false);
  75. PP.Initialize(*Target);
  76. PPConditionalDirectiveRecord *
  77. PPRec = new PPConditionalDirectiveRecord(SourceMgr);
  78. PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
  79. PP.EnterMainSourceFile();
  80. std::vector<Token> toks;
  81. while (1) {
  82. Token tok;
  83. PP.Lex(tok);
  84. if (tok.is(tok::eof))
  85. break;
  86. toks.push_back(tok);
  87. }
  88. // Make sure we got the tokens that we expected.
  89. ASSERT_EQ(10U, toks.size());
  90. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  91. SourceRange(toks[0].getLocation(), toks[1].getLocation())));
  92. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  93. SourceRange(toks[0].getLocation(), toks[2].getLocation())));
  94. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  95. SourceRange(toks[3].getLocation(), toks[4].getLocation())));
  96. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  97. SourceRange(toks[1].getLocation(), toks[5].getLocation())));
  98. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  99. SourceRange(toks[2].getLocation(), toks[6].getLocation())));
  100. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  101. SourceRange(toks[2].getLocation(), toks[5].getLocation())));
  102. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  103. SourceRange(toks[0].getLocation(), toks[6].getLocation())));
  104. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  105. SourceRange(toks[2].getLocation(), toks[8].getLocation())));
  106. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  107. SourceRange(toks[0].getLocation(), toks[9].getLocation())));
  108. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  109. toks[0].getLocation(), toks[2].getLocation()));
  110. EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
  111. toks[3].getLocation(), toks[4].getLocation()));
  112. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  113. toks[1].getLocation(), toks[5].getLocation()));
  114. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  115. toks[2].getLocation(), toks[0].getLocation()));
  116. EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
  117. toks[4].getLocation(), toks[3].getLocation()));
  118. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  119. toks[5].getLocation(), toks[1].getLocation()));
  120. }
  121. } // anonymous namespace