PPConditionalDirectiveRecordTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 llvm;
  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. class VoidModuleLoader : public ModuleLoader {
  49. ModuleLoadResult loadModule(SourceLocation ImportLoc,
  50. ModuleIdPath Path,
  51. Module::NameVisibilityKind Visibility,
  52. bool IsInclusionDirective) override {
  53. return ModuleLoadResult();
  54. }
  55. void makeModuleVisible(Module *Mod,
  56. Module::NameVisibilityKind Visibility,
  57. SourceLocation ImportLoc,
  58. bool Complain) override { }
  59. GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
  60. { return nullptr; }
  61. bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
  62. { return 0; };
  63. };
  64. TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
  65. const char *source =
  66. "0 1\n"
  67. "#if 1\n"
  68. "2\n"
  69. "#ifndef BB\n"
  70. "3 4\n"
  71. "#else\n"
  72. "#endif\n"
  73. "5\n"
  74. "#endif\n"
  75. "6\n"
  76. "#if 1\n"
  77. "7\n"
  78. "#if 1\n"
  79. "#endif\n"
  80. "8\n"
  81. "#endif\n"
  82. "9\n";
  83. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(source);
  84. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
  85. VoidModuleLoader ModLoader;
  86. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  87. Target.get());
  88. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  89. HeaderInfo, ModLoader,
  90. /*IILookup =*/nullptr,
  91. /*OwnsHeaderSearch =*/false);
  92. PP.Initialize(*Target);
  93. PPConditionalDirectiveRecord *
  94. PPRec = new PPConditionalDirectiveRecord(SourceMgr);
  95. PP.addPPCallbacks(PPRec);
  96. PP.EnterMainSourceFile();
  97. std::vector<Token> toks;
  98. while (1) {
  99. Token tok;
  100. PP.Lex(tok);
  101. if (tok.is(tok::eof))
  102. break;
  103. toks.push_back(tok);
  104. }
  105. // Make sure we got the tokens that we expected.
  106. ASSERT_EQ(10U, toks.size());
  107. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  108. SourceRange(toks[0].getLocation(), toks[1].getLocation())));
  109. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  110. SourceRange(toks[0].getLocation(), toks[2].getLocation())));
  111. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  112. SourceRange(toks[3].getLocation(), toks[4].getLocation())));
  113. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  114. SourceRange(toks[1].getLocation(), toks[5].getLocation())));
  115. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  116. SourceRange(toks[2].getLocation(), toks[6].getLocation())));
  117. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  118. SourceRange(toks[2].getLocation(), toks[5].getLocation())));
  119. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  120. SourceRange(toks[0].getLocation(), toks[6].getLocation())));
  121. EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
  122. SourceRange(toks[2].getLocation(), toks[8].getLocation())));
  123. EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
  124. SourceRange(toks[0].getLocation(), toks[9].getLocation())));
  125. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  126. toks[0].getLocation(), toks[2].getLocation()));
  127. EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
  128. toks[3].getLocation(), toks[4].getLocation()));
  129. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  130. toks[1].getLocation(), toks[5].getLocation()));
  131. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  132. toks[2].getLocation(), toks[0].getLocation()));
  133. EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
  134. toks[4].getLocation(), toks[3].getLocation()));
  135. EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
  136. toks[5].getLocation(), toks[1].getLocation()));
  137. }
  138. } // anonymous namespace