PPConditionalDirectiveRecordTest.cpp 5.0 KB

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