summaryrefslogtreecommitdiffstats
path: root/include/clang/Lex/PPConditionalDirectiveRecord.h
blob: a2ccf1407f79c87b872302c803204668120e52d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the PPConditionalDirectiveRecord class, which maintains
//  a record of conditional directive regions.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
#define LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H

#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/PPCallbacks.h"
#include "llvm/ADT/SmallVector.h"
#include <vector>

namespace clang {

/// Records preprocessor conditional directive regions and allows
/// querying in which region source locations belong to.
class PPConditionalDirectiveRecord : public PPCallbacks {
  SourceManager &SourceMgr;

  SmallVector<SourceLocation, 6> CondDirectiveStack;

  class CondDirectiveLoc {
    SourceLocation Loc;
    SourceLocation RegionLoc;

  public:
    CondDirectiveLoc(SourceLocation Loc, SourceLocation RegionLoc)
      : Loc(Loc), RegionLoc(RegionLoc) {}

    SourceLocation getLoc() const { return Loc; }
    SourceLocation getRegionLoc() const { return RegionLoc; }

    class Comp {
      SourceManager &SM;
    public:
      explicit Comp(SourceManager &SM) : SM(SM) {}
      bool operator()(const CondDirectiveLoc &LHS,
                      const CondDirectiveLoc &RHS) {
        return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
      }
      bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
        return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
      }
      bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
        return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
      }
    };
  };

  typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
  /// The locations of conditional directives in source order.
  CondDirectiveLocsTy CondDirectiveLocs;

  void addCondDirectiveLoc(CondDirectiveLoc DirLoc);

public:
  /// Construct a new preprocessing record.
  explicit PPConditionalDirectiveRecord(SourceManager &SM);

  size_t getTotalMemory() const;

  SourceManager &getSourceManager() const { return SourceMgr; }

  /// Returns true if the given range intersects with a conditional
  /// directive. if a \#if/\#endif block is fully contained within the range,
  /// this function will return false.
  bool rangeIntersectsConditionalDirective(SourceRange Range) const;

  /// Returns true if the given locations are in different regions,
  /// separated by conditional directive blocks.
  bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS,
                                                SourceLocation RHS) const {
    return findConditionalDirectiveRegionLoc(LHS) !=
        findConditionalDirectiveRegionLoc(RHS);
  }

  SourceLocation findConditionalDirectiveRegionLoc(SourceLocation Loc) const;

private:
  void If(SourceLocation Loc, SourceRange ConditionRange,
          ConditionValueKind ConditionValue) override;
  void Elif(SourceLocation Loc, SourceRange ConditionRange,
            ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
  void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
             const MacroDefinition &MD) override;
  void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
              const MacroDefinition &MD) override;
  void Else(SourceLocation Loc, SourceLocation IfLoc) override;
  void Endif(SourceLocation Loc, SourceLocation IfLoc) override;
};

} // end namespace clang

#endif // LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H