PPConditionalDirectiveRecordTest.cpp 5.6 KB

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