Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Lex/MultipleIncludeOpt.h
Line
Count
Source
1
//===--- MultipleIncludeOpt.h - Header Multiple-Include Optzn ---*- C++ -*-===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// \file
10
/// Defines the MultipleIncludeOpt interface.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_LEX_MULTIPLEINCLUDEOPT_H
15
#define LLVM_CLANG_LEX_MULTIPLEINCLUDEOPT_H
16
17
#include "clang/Basic/SourceLocation.h"
18
19
namespace clang {
20
class IdentifierInfo;
21
22
/// Implements the simple state machine that the Lexer class uses to
23
/// detect files subject to the 'multiple-include' optimization.
24
///
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.
34
  bool ReadAnyTokens;
35
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;
40
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;
47
48
  /// TheMacro - The controlling macro for a file, if valid.
49
  ///
50
  const IdentifierInfo *TheMacro;
51
52
  /// DefinedMacro - The macro defined right after TheMacro, if any.
53
  const IdentifierInfo *DefinedMacro;
54
55
  SourceLocation MacroLoc;
56
  SourceLocation DefinedLoc;
57
public:
58
31.6M
  MultipleIncludeOpt() {
59
31.6M
    ReadAnyTokens = false;
60
31.6M
    ImmediatelyAfterTopLevelIfndef = false;
61
31.6M
    DidMacroExpansion = false;
62
31.6M
    TheMacro = nullptr;
63
31.6M
    DefinedMacro = nullptr;
64
31.6M
  }
65
66
12
  SourceLocation GetMacroLocation() const {
67
12
    return MacroLoc;
68
12
  }
69
70
18
  SourceLocation GetDefinedLocation() const {
71
18
    return DefinedLoc;
72
18
  }
73
74
48.9M
  void resetImmediatelyAfterTopLevelIfndef() {
75
48.9M
    ImmediatelyAfterTopLevelIfndef = false;
76
48.9M
  }
77
78
509k
  void SetDefinedMacro(IdentifierInfo *M, SourceLocation Loc) {
79
509k
    DefinedMacro = M;
80
509k
    DefinedLoc = Loc;
81
509k
  }
82
83
  /// Invalidate - Permanently mark this file as not being suitable for the
84
  /// include-file optimization.
85
964k
  void Invalidate() {
86
964k
    // If we have read tokens but have no controlling macro, the state-machine
87
964k
    // below can never "accept".
88
964k
    ReadAnyTokens = true;
89
964k
    ImmediatelyAfterTopLevelIfndef = false;
90
964k
    DefinedMacro = nullptr;
91
964k
    TheMacro = nullptr;
92
964k
  }
93
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
48.9M
  bool getHasReadAnyTokensVal() const { return ReadAnyTokens; }
98
99
  /// getImmediatelyAfterTopLevelIfndef - returns true if the last directive
100
  /// was an #ifndef at the beginning of the file.
101
48.9M
  bool getImmediatelyAfterTopLevelIfndef() const {
102
48.9M
    return ImmediatelyAfterTopLevelIfndef;
103
48.9M
  }
104
105
  // If a token is read, remember that we have seen a side-effect in this file.
106
1.93G
  void ReadToken() {
107
1.93G
    ReadAnyTokens = true;
108
1.93G
    ImmediatelyAfterTopLevelIfndef = false;
109
1.93G
  }
110
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
24.7M
  void ExpandedMacro() { DidMacroExpansion = true; }
114
115
  /// Called when entering a top-level \#ifndef directive (or the
116
  /// "\#if !defined" equivalent) without any preceding tokens.
117
  ///
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
579k
  void EnterTopLevelIfndef(const IdentifierInfo *M, SourceLocation Loc) {
123
579k
    // If the macro is already set, this is after the top-level #endif.
124
579k
    if (TheMacro)
125
18
      return Invalidate();
126
579k
127
579k
    // If we have already expanded a macro by the end of the #ifndef line, then
128
579k
    // there is a macro expansion *in* the #ifndef line.  This means that the
129
579k
    // condition could evaluate differently when subsequently #included.  Reject
130
579k
    // this.
131
579k
    if (DidMacroExpansion)
132
1
      return Invalidate();
133
579k
134
579k
    // Remember that we're in the #if and that we have the macro.
135
579k
    ReadAnyTokens = true;
136
579k
    ImmediatelyAfterTopLevelIfndef = true;
137
579k
    TheMacro = M;
138
579k
    MacroLoc = Loc;
139
579k
  }
140
141
  /// Invoked when a top level conditional (except \#ifndef) is found.
142
669k
  void EnterTopLevelConditional() {
143
669k
    // If a conditional directive (except #ifndef) is found at the top level,
144
669k
    // there is a chunk of the file not guarded by the controlling macro.
145
669k
    Invalidate();
146
669k
  }
147
148
  /// Called when the lexer exits the top-level conditional.
149
873k
  void ExitTopLevelConditional() {
150
873k
    // If we have a macro, that means the top of the file was ok.  Set our state
151
873k
    // back to "not having read any tokens" so we can detect anything after the
152
873k
    // #endif.
153
873k
    if (!TheMacro) 
return Invalidate()295k
;
154
578k
155
578k
    // At this point, we haven't "read any tokens" but we do have a controlling
156
578k
    // macro.
157
578k
    ReadAnyTokens = false;
158
578k
    ImmediatelyAfterTopLevelIfndef = false;
159
578k
  }
160
161
  /// Once the entire file has been lexed, if there is a controlling
162
  /// macro, return it.
163
1.52M
  const IdentifierInfo *GetControllingMacroAtEndOfFile() const {
164
1.52M
    // If we haven't read any tokens after the #endif, return the controlling
165
1.52M
    // macro if it's valid (if it isn't, it will be null).
166
1.52M
    if (!ReadAnyTokens)
167
607k
      return TheMacro;
168
916k
    return nullptr;
169
916k
  }
170
171
  /// If the ControllingMacro is followed by a macro definition, return
172
  /// the macro that was defined.
173
577k
  const IdentifierInfo *GetDefinedMacro() const {
174
577k
    return DefinedMacro;
175
577k
  }
176
};
177
178
}  // end namespace clang
179
180
#endif