PPConditionalDirectiveRecord.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the PPConditionalDirectiveRecord class, which maintains
  15. // a record of conditional directive regions.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
  19. #define LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
  20. #include "clang/Basic/SourceLocation.h"
  21. #include "clang/Lex/PPCallbacks.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include <vector>
  24. namespace clang {
  25. /// Records preprocessor conditional directive regions and allows
  26. /// querying in which region source locations belong to.
  27. class PPConditionalDirectiveRecord : public PPCallbacks {
  28. SourceManager &SourceMgr;
  29. SmallVector<SourceLocation, 6> CondDirectiveStack;
  30. class CondDirectiveLoc {
  31. SourceLocation Loc;
  32. SourceLocation RegionLoc;
  33. public:
  34. CondDirectiveLoc(SourceLocation Loc, SourceLocation RegionLoc)
  35. : Loc(Loc), RegionLoc(RegionLoc) {}
  36. SourceLocation getLoc() const { return Loc; }
  37. SourceLocation getRegionLoc() const { return RegionLoc; }
  38. class Comp {
  39. SourceManager &SM;
  40. public:
  41. explicit Comp(SourceManager &SM) : SM(SM) {}
  42. bool operator()(const CondDirectiveLoc &LHS,
  43. const CondDirectiveLoc &RHS) {
  44. return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
  45. }
  46. bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
  47. return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
  48. }
  49. bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
  50. return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
  51. }
  52. };
  53. };
  54. typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
  55. /// The locations of conditional directives in source order.
  56. CondDirectiveLocsTy CondDirectiveLocs;
  57. void addCondDirectiveLoc(CondDirectiveLoc DirLoc);
  58. public:
  59. /// Construct a new preprocessing record.
  60. explicit PPConditionalDirectiveRecord(SourceManager &SM);
  61. size_t getTotalMemory() const;
  62. SourceManager &getSourceManager() const { return SourceMgr; }
  63. /// Returns true if the given range intersects with a conditional
  64. /// directive. if a \#if/\#endif block is fully contained within the range,
  65. /// this function will return false.
  66. bool rangeIntersectsConditionalDirective(SourceRange Range) const;
  67. /// Returns true if the given locations are in different regions,
  68. /// separated by conditional directive blocks.
  69. bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS,
  70. SourceLocation RHS) const {
  71. return findConditionalDirectiveRegionLoc(LHS) !=
  72. findConditionalDirectiveRegionLoc(RHS);
  73. }
  74. SourceLocation findConditionalDirectiveRegionLoc(SourceLocation Loc) const;
  75. private:
  76. void If(SourceLocation Loc, SourceRange ConditionRange,
  77. ConditionValueKind ConditionValue) override;
  78. void Elif(SourceLocation Loc, SourceRange ConditionRange,
  79. ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
  80. void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
  81. const MacroDefinition &MD) override;
  82. void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
  83. const MacroDefinition &MD) override;
  84. void Elifdef(SourceLocation Loc, const Token &MacroNameTok,
  85. const MacroDefinition &MD) override;
  86. void Elifdef(SourceLocation Loc, SourceRange ConditionRange,
  87. SourceLocation IfLoc) override;
  88. void Elifndef(SourceLocation Loc, const Token &MacroNameTok,
  89. const MacroDefinition &MD) override;
  90. void Elifndef(SourceLocation Loc, SourceRange ConditionRange,
  91. SourceLocation IfLoc) override;
  92. void Else(SourceLocation Loc, SourceLocation IfLoc) override;
  93. void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
  94. };
  95. } // end namespace clang
  96. #endif // LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
  97. #ifdef __GNUC__
  98. #pragma GCC diagnostic pop
  99. #endif