1 //===--- MultipleIncludeOpt.h - Header Multiple-Include Optzn ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Defines the MultipleIncludeOpt interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_MULTIPLEINCLUDEOPT_H
15 #define LLVM_CLANG_LEX_MULTIPLEINCLUDEOPT_H
17 #include "clang/Basic/SourceLocation.h"
22 /// Implements the simple state machine that the Lexer class uses to
23 /// detect files subject to the 'multiple-include' optimization.
25 /// The public methods in this class are triggered by various
26 /// events that occur when a file is lexed, and after the entire file is lexed,
27 /// information about which macro (if any) controls the header is returned.
28 class MultipleIncludeOpt {
29 /// ReadAnyTokens - This is set to false when a file is first opened and true
30 /// any time a token is returned to the client or a (non-multiple-include)
31 /// directive is parsed. When the final \#endif is parsed this is reset back
32 /// to false, that way any tokens before the first \#ifdef or after the last
33 /// \#endif can be easily detected.
36 /// ImmediatelyAfterTopLevelIfndef - This is true when the only tokens
37 /// processed in the file so far is an #ifndef and an identifier. Used in
38 /// the detection of header guards in a file.
39 bool ImmediatelyAfterTopLevelIfndef;
41 /// ReadAnyTokens - This is set to false when a file is first opened and true
42 /// any time a token is returned to the client or a (non-multiple-include)
43 /// directive is parsed. When the final #endif is parsed this is reset back
44 /// to false, that way any tokens before the first #ifdef or after the last
45 /// #endif can be easily detected.
46 bool DidMacroExpansion;
48 /// TheMacro - The controlling macro for a file, if valid.
50 const IdentifierInfo *TheMacro;
52 /// DefinedMacro - The macro defined right after TheMacro, if any.
53 const IdentifierInfo *DefinedMacro;
55 SourceLocation MacroLoc;
56 SourceLocation DefinedLoc;
58 MultipleIncludeOpt() {
59 ReadAnyTokens = false;
60 ImmediatelyAfterTopLevelIfndef = false;
61 DidMacroExpansion = false;
63 DefinedMacro = nullptr;
66 SourceLocation GetMacroLocation() const {
70 SourceLocation GetDefinedLocation() const {
74 void resetImmediatelyAfterTopLevelIfndef() {
75 ImmediatelyAfterTopLevelIfndef = false;
78 void SetDefinedMacro(IdentifierInfo *M, SourceLocation Loc) {
83 /// Invalidate - Permanently mark this file as not being suitable for the
84 /// include-file optimization.
86 // If we have read tokens but have no controlling macro, the state-machine
87 // below can never "accept".
89 ImmediatelyAfterTopLevelIfndef = false;
90 DefinedMacro = nullptr;
94 /// getHasReadAnyTokensVal - This is used for the \#ifndef handshake at the
95 /// top of the file when reading preprocessor directives. Otherwise, reading
96 /// the "ifndef x" would count as reading tokens.
97 bool getHasReadAnyTokensVal() const { return ReadAnyTokens; }
99 /// getImmediatelyAfterTopLevelIfndef - returns true if the last directive
100 /// was an #ifndef at the beginning of the file.
101 bool getImmediatelyAfterTopLevelIfndef() const {
102 return ImmediatelyAfterTopLevelIfndef;
105 // If a token is read, remember that we have seen a side-effect in this file.
107 ReadAnyTokens = true;
108 ImmediatelyAfterTopLevelIfndef = false;
111 /// ExpandedMacro - When a macro is expanded with this lexer as the current
112 /// buffer, this method is called to disable the MIOpt if needed.
113 void ExpandedMacro() { DidMacroExpansion = true; }
115 /// Called when entering a top-level \#ifndef directive (or the
116 /// "\#if !defined" equivalent) without any preceding tokens.
118 /// Note, we don't care about the input value of 'ReadAnyTokens'. The caller
119 /// ensures that this is only called if there are no tokens read before the
120 /// \#ifndef. The caller is required to do this, because reading the \#if
121 /// line obviously reads in tokens.
122 void EnterTopLevelIfndef(const IdentifierInfo *M, SourceLocation Loc) {
123 // If the macro is already set, this is after the top-level #endif.
127 // If we have already expanded a macro by the end of the #ifndef line, then
128 // there is a macro expansion *in* the #ifndef line. This means that the
129 // condition could evaluate differently when subsequently #included. Reject
131 if (DidMacroExpansion)
134 // Remember that we're in the #if and that we have the macro.
135 ReadAnyTokens = true;
136 ImmediatelyAfterTopLevelIfndef = true;
141 /// Invoked when a top level conditional (except \#ifndef) is found.
142 void EnterTopLevelConditional() {
143 // If a conditional directive (except #ifndef) is found at the top level,
144 // there is a chunk of the file not guarded by the controlling macro.
148 /// Called when the lexer exits the top-level conditional.
149 void ExitTopLevelConditional() {
150 // If we have a macro, that means the top of the file was ok. Set our state
151 // back to "not having read any tokens" so we can detect anything after the
153 if (!TheMacro) return Invalidate();
155 // At this point, we haven't "read any tokens" but we do have a controlling
157 ReadAnyTokens = false;
158 ImmediatelyAfterTopLevelIfndef = false;
161 /// Once the entire file has been lexed, if there is a controlling
162 /// macro, return it.
163 const IdentifierInfo *GetControllingMacroAtEndOfFile() const {
164 // If we haven't read any tokens after the #endif, return the controlling
165 // macro if it's valid (if it isn't, it will be null).
171 /// If the ControllingMacro is followed by a macro definition, return
172 /// the macro that was defined.
173 const IdentifierInfo *GetDefinedMacro() const {
178 } // end namespace clang