MultipleIncludeOpt.h revision 5f016e2cb5d11daeb237544de1c5d59f20fe1a6e
1//===--- MultipleIncludeOpt.h - Header Multiple-Include Optzn ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the MultipleIncludeOpt interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_MULTIPLEINCLUDEOPT_H
15#define LLVM_CLANG_MULTIPLEINCLUDEOPT_H
16
17namespace clang {
18class IdentifierInfo;
19
20/// MultipleIncludeOpt - This class implements the simple state machine that the
21/// Lexer class uses to detect files subject to the 'multiple-include'
22/// optimization.  The public methods in this class are triggered by various
23/// events that occur when a file is lexed, and after the entire file is lexed,
24/// information about which macro (if any) controls the header is returned.
25class MultipleIncludeOpt {
26  /// ReadAnyTokens - This is set to false when a file is first opened and true
27  /// any time a token is returned to the client or a (non-multiple-include)
28  /// directive is parsed.  When the final #endif is parsed this is reset back
29  /// to false, that way any tokens before the first #ifdef or after the last
30  /// #endif can be easily detected.
31  bool ReadAnyTokens;
32
33  /// TheMacro - The controlling macro for a file, if valid.
34  ///
35  const IdentifierInfo *TheMacro;
36public:
37  MultipleIncludeOpt() : ReadAnyTokens(false), TheMacro(0) {}
38
39  /// Invalidate - Permenantly mark this file as not being suitable for the
40  /// include-file optimization.
41  void Invalidate() {
42    // If we have read tokens but have no controlling macro, the state-machine
43    // below can never "accept".
44    ReadAnyTokens = true;
45    TheMacro = 0;
46  }
47
48  /// getHasReadAnyTokensVal - This is used for the #ifndef hande-shake at the
49  /// top of the file when reading preprocessor directives.  Otherwise, reading
50  /// the "ifndef x" would count as reading tokens.
51  bool getHasReadAnyTokensVal() const { return ReadAnyTokens; }
52
53  // If a token is read, remember that we have seen a side-effect in this file.
54  void ReadToken() { ReadAnyTokens = true; }
55
56  /// EnterTopLevelIFNDEF - When entering a top-level #ifndef directive (or the
57  /// "#if !defined" equivalent) without any preceding tokens, this method is
58  /// called.
59  void EnterTopLevelIFNDEF(const IdentifierInfo *M) {
60    // Note, we don't care about the input value of 'ReadAnyTokens'.  The caller
61    // ensures that this is only called if there are no tokens read before the
62    // #ifndef.
63
64    // If the macro is already set, this is after the top-level #endif.
65    if (TheMacro)
66      return Invalidate();
67
68    // Remember that we're in the #if and that we have the macro.
69    ReadAnyTokens = true;
70    TheMacro = M;
71  }
72
73  /// FoundTopLevelElse - This is invoked when an #else/#elif directive is found
74  /// in the top level conditional in the file.
75  void FoundTopLevelElse() {
76    /// If a #else directive is found at the top level, there is a chunk of the
77    /// file not guarded by the controlling macro.
78    Invalidate();
79  }
80
81  /// ExitTopLevelConditional - This method is called when the lexer exits the
82  /// top-level conditional.
83  void ExitTopLevelConditional() {
84    // If we have a macro, that means the top of the file was ok.  Set our state
85    // back to "not having read any tokens" so we can detect anything after the
86    // #endif.
87    if (!TheMacro) return Invalidate();
88
89    // At this point, we haven't "read any tokens" but we do have a controlling
90    // macro.
91    ReadAnyTokens = false;
92  }
93
94  /// GetControllingMacroAtEndOfFile - Once the entire file has been lexed, if
95  /// there is a controlling macro, return it.
96  const IdentifierInfo *GetControllingMacroAtEndOfFile() const {
97    // If we haven't read any tokens after the #endif, return the controlling
98    // macro if it's valid (if it isn't, it will be null).
99    if (!ReadAnyTokens)
100      return TheMacro;
101    return 0;
102  }
103};
104
105}  // end namespace clang
106
107#endif
108