Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/MacroInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MacroInfo.cpp - Information about #defined identifiers -------------===//
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
// This file implements the MacroInfo interface.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Lex/MacroInfo.h"
14
#include "clang/Basic/IdentifierTable.h"
15
#include "clang/Basic/LLVM.h"
16
#include "clang/Basic/SourceLocation.h"
17
#include "clang/Basic/SourceManager.h"
18
#include "clang/Basic/TokenKinds.h"
19
#include "clang/Lex/Preprocessor.h"
20
#include "clang/Lex/Token.h"
21
#include "llvm/ADT/StringRef.h"
22
#include "llvm/Support/Casting.h"
23
#include "llvm/Support/Compiler.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include <cassert>
26
#include <optional>
27
#include <utility>
28
29
using namespace clang;
30
31
namespace {
32
33
// MacroInfo is expected to take 40 bytes on platforms with an 8 byte pointer
34
// and 4 byte SourceLocation.
35
template <int> class MacroInfoSizeChecker {
36
public:
37
  [[maybe_unused]] constexpr static bool AsExpected = true;
38
};
39
template <> class MacroInfoSizeChecker<8> {
40
public:
41
  [[maybe_unused]] constexpr static bool AsExpected =
42
      sizeof(MacroInfo) == (32 + sizeof(SourceLocation) * 2);
43
};
44
45
static_assert(MacroInfoSizeChecker<sizeof(void *)>::AsExpected,
46
              "Unexpected size of MacroInfo");
47
48
} // end namespace
49
50
MacroInfo::MacroInfo(SourceLocation DefLoc)
51
53.8M
    : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
52
53.8M
      IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
53
53.8M
      HasCommaPasting(false), IsDisabled(false), IsUsed(false),
54
53.8M
      IsAllowRedefinitionsWithoutWarning(false), IsWarnIfUnused(false),
55
53.8M
      UsedForHeaderGuard(false) {}
56
57
568k
unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const {
58
568k
  assert(!IsDefinitionLengthCached);
59
568k
  IsDefinitionLengthCached = true;
60
61
568k
  ArrayRef<Token> ReplacementTokens = tokens();
62
568k
  if (ReplacementTokens.empty())
63
0
    return (DefinitionLength = 0);
64
65
568k
  const Token &firstToken = ReplacementTokens.front();
66
568k
  const Token &lastToken = ReplacementTokens.back();
67
568k
  SourceLocation macroStart = firstToken.getLocation();
68
568k
  SourceLocation macroEnd = lastToken.getLocation();
69
568k
  assert(macroStart.isValid() && macroEnd.isValid());
70
568k
  assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
71
568k
         "Macro defined in macro?");
72
568k
  assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
73
568k
         "Macro defined in macro?");
74
568k
  std::pair<FileID, unsigned>
75
568k
      startInfo = SM.getDecomposedExpansionLoc(macroStart);
76
568k
  std::pair<FileID, unsigned>
77
568k
      endInfo = SM.getDecomposedExpansionLoc(macroEnd);
78
568k
  assert(startInfo.first == endInfo.first &&
79
568k
         "Macro definition spanning multiple FileIDs ?");
80
568k
  assert(startInfo.second <= endInfo.second);
81
568k
  DefinitionLength = endInfo.second - startInfo.second;
82
568k
  DefinitionLength += lastToken.getLength();
83
84
568k
  return DefinitionLength;
85
568k
}
86
87
/// Return true if the specified macro definition is equal to
88
/// this macro in spelling, arguments, and whitespace.
89
///
90
/// \param Syntactically if true, the macro definitions can be identical even
91
/// if they use different identifiers for the function macro parameters.
92
/// Otherwise the comparison is lexical and this implements the rules in
93
/// C99 6.10.3.
94
bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
95
1.03M
                              bool Syntactically) const {
96
1.03M
  bool Lexically = !Syntactically;
97
98
  // Check # tokens in replacement, number of args, and various flags all match.
99
1.03M
  if (getNumTokens() != Other.getNumTokens() ||
100
1.03M
      
getNumParams() != Other.getNumParams()1.03M
||
101
1.03M
      
isFunctionLike() != Other.isFunctionLike()1.03M
||
102
1.03M
      
isC99Varargs() != Other.isC99Varargs()1.03M
||
103
1.03M
      
isGNUVarargs() != Other.isGNUVarargs()1.03M
)
104
141
    return false;
105
106
1.03M
  if (Lexically) {
107
    // Check arguments.
108
300k
    for (param_iterator I = param_begin(), OI = Other.param_begin(),
109
300k
                        E = param_end();
110
300k
         I != E; 
++I, ++OI25
)
111
26
      if (*I != *OI) 
return false1
;
112
300k
  }
113
114
  // Check all the tokens.
115
5.03M
  
for (unsigned i = 0; 1.03M
i != NumReplacementTokens;
++i3.99M
) {
116
3.99M
    const Token &A = ReplacementTokens[i];
117
3.99M
    const Token &B = Other.ReplacementTokens[i];
118
3.99M
    if (A.getKind() != B.getKind())
119
87
      return false;
120
121
    // If this isn't the first token, check that the whitespace and
122
    // start-of-line characteristics match.
123
3.99M
    if (i != 0 &&
124
3.99M
        
(3.01M
A.isAtStartOfLine() != B.isAtStartOfLine()3.01M
||
125
3.01M
         A.hasLeadingSpace() != B.hasLeadingSpace()))
126
2
      return false;
127
128
    // If this is an identifier, it is easy.
129
3.99M
    if (A.getIdentifierInfo() || 
B.getIdentifierInfo()2.56M
) {
130
1.42M
      if (A.getIdentifierInfo() == B.getIdentifierInfo())
131
1.42M
        continue;
132
21
      if (Lexically)
133
0
        return false;
134
      // With syntactic equivalence the parameter names can be different as long
135
      // as they are used in the same place.
136
21
      int AArgNum = getParameterNum(A.getIdentifierInfo());
137
21
      if (AArgNum == -1)
138
0
        return false;
139
21
      if (AArgNum != Other.getParameterNum(B.getIdentifierInfo()))
140
1
        return false;
141
20
      continue;
142
21
    }
143
144
    // Otherwise, check the spelling.
145
2.56M
    if (PP.getSpelling(A) != PP.getSpelling(B))
146
41
      return false;
147
2.56M
  }
148
149
1.03M
  return true;
150
1.03M
}
151
152
19
LLVM_DUMP_METHOD void MacroInfo::dump() const {
153
19
  llvm::raw_ostream &Out = llvm::errs();
154
155
  // FIXME: Dump locations.
156
19
  Out << "MacroInfo " << this;
157
19
  if (IsBuiltinMacro) 
Out << " builtin"0
;
158
19
  if (IsDisabled) 
Out << " disabled"0
;
159
19
  if (IsUsed) 
Out << " used"2
;
160
19
  if (IsAllowRedefinitionsWithoutWarning)
161
0
    Out << " allow_redefinitions_without_warning";
162
19
  if (IsWarnIfUnused) 
Out << " warn_if_unused"0
;
163
19
  if (UsedForHeaderGuard) 
Out << " header_guard"1
;
164
165
19
  Out << "\n    #define <macro>";
166
19
  if (IsFunctionLike) {
167
0
    Out << "(";
168
0
    for (unsigned I = 0; I != NumParameters; ++I) {
169
0
      if (I) Out << ", ";
170
0
      Out << ParameterList[I]->getName();
171
0
    }
172
0
    if (IsC99Varargs || IsGNUVarargs) {
173
0
      if (NumParameters && IsC99Varargs) Out << ", ";
174
0
      Out << "...";
175
0
    }
176
0
    Out << ")";
177
0
  }
178
179
19
  bool First = true;
180
19
  for (const Token &Tok : tokens()) {
181
    // Leading space is semantically meaningful in a macro definition,
182
    // so preserve it in the dump output.
183
18
    if (First || 
Tok.hasLeadingSpace()0
)
184
18
      Out << " ";
185
18
    First = false;
186
187
18
    if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
188
0
      Out << Punc;
189
18
    else if (Tok.isLiteral() && Tok.getLiteralData())
190
0
      Out << StringRef(Tok.getLiteralData(), Tok.getLength());
191
18
    else if (auto *II = Tok.getIdentifierInfo())
192
0
      Out << II->getName();
193
18
    else
194
18
      Out << Tok.getName();
195
18
  }
196
19
}
197
198
326M
MacroDirective::DefInfo MacroDirective::getDefinition() {
199
326M
  MacroDirective *MD = this;
200
326M
  SourceLocation UndefLoc;
201
326M
  std::optional<bool> isPublic;
202
326M
  for (; MD; 
MD = MD->getPrevious()219k
) {
203
326M
    if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
204
326M
      return DefInfo(DefMD, UndefLoc, !isPublic || 
*isPublic350
);
205
206
219k
    
if (UndefMacroDirective *219k
UndefMD219k
= dyn_cast<UndefMacroDirective>(MD)) {
207
219k
      UndefLoc = UndefMD->getLocation();
208
219k
      continue;
209
219k
    }
210
211
18.4E
    VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
212
18.4E
    if (!isPublic)
213
350
      isPublic = VisMD->isPublic();
214
18.4E
  }
215
216
141
  return DefInfo(nullptr, UndefLoc, !isPublic || 
*isPublic0
);
217
326M
}
218
219
const MacroDirective::DefInfo
220
MacroDirective::findDirectiveAtLoc(SourceLocation L,
221
166k
                                   const SourceManager &SM) const {
222
166k
  assert(L.isValid() && "SourceLocation is invalid.");
223
167k
  
for (DefInfo Def = getDefinition(); 166k
Def;
Def = Def.getPreviousDefinition()270
) {
224
167k
    if (Def.getLocation().isInvalid() ||  // For macros defined on the command line.
225
167k
        
SM.isBeforeInTranslationUnit(Def.getLocation(), L)155k
)
226
166k
      return (!Def.isUndefined() ||
227
166k
              
SM.isBeforeInTranslationUnit(L, Def.getUndefLocation())270
)
228
166k
                  ? 
Def166k
:
DefInfo()213
;
229
167k
  }
230
153
  return DefInfo();
231
166k
}
232
233
0
LLVM_DUMP_METHOD void MacroDirective::dump() const {
234
0
  llvm::raw_ostream &Out = llvm::errs();
235
236
0
  switch (getKind()) {
237
0
  case MD_Define: Out << "DefMacroDirective"; break;
238
0
  case MD_Undefine: Out << "UndefMacroDirective"; break;
239
0
  case MD_Visibility: Out << "VisibilityMacroDirective"; break;
240
0
  }
241
0
  Out << " " << this;
242
  // FIXME: Dump SourceLocation.
243
0
  if (auto *Prev = getPrevious())
244
0
    Out << " prev " << Prev;
245
0
  if (IsFromPCH) Out << " from_pch";
246
247
0
  if (isa<VisibilityMacroDirective>(this))
248
0
    Out << (IsPublic ? " public" : " private");
249
250
0
  if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
251
0
    if (auto *Info = DMD->getInfo()) {
252
0
      Out << "\n  ";
253
0
      Info->dump();
254
0
    }
255
0
  }
256
0
  Out << "\n";
257
0
}
258
259
ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
260
                                 IdentifierInfo *II, MacroInfo *Macro,
261
6.19M
                                 ArrayRef<ModuleMacro *> Overrides) {
262
6.19M
  void *Mem = PP.getPreprocessorAllocator().Allocate(
263
6.19M
      sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
264
6.19M
      alignof(ModuleMacro));
265
6.19M
  return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);
266
6.19M
}