PPConditionalDirectiveRecordTest.cpp 5.6 KB

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