Coverage Report

Created: 2021-03-06 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/Types.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Types.cpp - Driver input & temporary type information ------------===//
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
#include "clang/Driver/Types.h"
10
#include "clang/Driver/Driver.h"
11
#include "clang/Driver/DriverDiagnostic.h"
12
#include "clang/Driver/Options.h"
13
#include "llvm/ADT/STLExtras.h"
14
#include "llvm/ADT/SmallVector.h"
15
#include "llvm/ADT/StringSwitch.h"
16
#include "llvm/Option/Arg.h"
17
#include <cassert>
18
#include <cstring>
19
20
using namespace clang::driver;
21
using namespace clang::driver::types;
22
23
struct TypeInfo {
24
  const char *Name;
25
  const char *TempSuffix;
26
  ID PreprocessedType;
27
  class PhasesBitSet {
28
    unsigned Bits = 0;
29
30
  public:
31
0
    constexpr PhasesBitSet(std::initializer_list<phases::ID> Phases) {
32
0
      for (auto Id : Phases)
33
0
        Bits |= 1 << Id;
34
0
    }
35
915k
    bool contains(phases::ID Id) const { return Bits & (1 << Id); }
36
  } Phases;
37
};
38
39
static constexpr TypeInfo TypeInfos[] = {
40
#define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, ...) \
41
  { NAME, TEMP_SUFFIX, TY_##PP_TYPE, { __VA_ARGS__ }, },
42
#include "clang/Driver/Types.def"
43
#undef TYPE
44
};
45
static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
46
47
394k
static const TypeInfo &getInfo(unsigned id) {
48
394k
  assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
49
0
  return TypeInfos[id - 1];
50
394k
}
51
52
43.0k
const char *types::getTypeName(ID Id) {
53
43.0k
  return getInfo(Id).Name;
54
43.0k
}
55
56
84.1k
types::ID types::getPreprocessedType(ID Id) {
57
84.1k
  ID PPT = getInfo(Id).PreprocessedType;
58
84.1k
  assert((getInfo(Id).Phases.contains(phases::Preprocess) !=
59
84.1k
          (PPT == TY_INVALID)) &&
60
84.1k
         "Unexpected Preprocess Type.");
61
0
  return PPT;
62
84.1k
}
63
64
174
static bool isPreprocessedModuleType(ID Id) {
65
174
  return Id == TY_CXXModule || Id == TY_PP_CXXModule;
66
174
}
67
68
86
types::ID types::getPrecompiledType(ID Id) {
69
86
  if (isPreprocessedModuleType(Id))
70
6
    return TY_ModuleFile;
71
80
  if (onlyPrecompileType(Id))
72
80
    return TY_PCH;
73
0
  return TY_INVALID;
74
80
}
75
76
11.1k
const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
77
11.1k
  if (CLMode) {
78
782
    switch (Id) {
79
451
    case TY_Object:
80
457
    case TY_LTO_BC:
81
457
      return "obj";
82
292
    case TY_Image:
83
292
      return "exe";
84
20
    case TY_PP_Asm:
85
20
      return "asm";
86
13
    default:
87
13
      break;
88
782
    }
89
782
  }
90
10.3k
  return getInfo(Id).TempSuffix;
91
11.1k
}
92
93
88
bool types::onlyPrecompileType(ID Id) {
94
88
  return getInfo(Id).Phases.contains(phases::Precompile) &&
95
88
         !isPreprocessedModuleType(Id);
96
88
}
97
98
39.2k
bool types::canTypeBeUserSpecified(ID Id) {
99
39.2k
  static const clang::driver::types::ID kStaticLangageTypes[] = {
100
39.2k
      TY_CUDA_DEVICE,   TY_HIP_DEVICE,    TY_PP_CHeader,
101
39.2k
      TY_PP_ObjCHeader, TY_PP_CXXHeader,  TY_PP_ObjCXXHeader,
102
39.2k
      TY_PP_CXXModule,  TY_LTO_IR,        TY_LTO_BC,
103
39.2k
      TY_Plist,         TY_RewrittenObjC, TY_RewrittenLegacyObjC,
104
39.2k
      TY_Remap,         TY_PCH,           TY_Object,
105
39.2k
      TY_Image,         TY_dSYM,          TY_Dependencies,
106
39.2k
      TY_CUDA_FATBIN,   TY_HIP_FATBIN};
107
39.2k
  return !llvm::is_contained(kStaticLangageTypes, Id);
108
39.2k
}
109
110
4.68k
bool types::appendSuffixForType(ID Id) {
111
4.68k
  return Id == TY_PCH || 
Id == TY_dSYM4.68k
||
Id == TY_CUDA_FATBIN4.59k
||
112
4.59k
         Id == TY_HIP_FATBIN;
113
4.68k
}
114
115
16
bool types::canLipoType(ID Id) {
116
16
  return (Id == TY_Nothing ||
117
16
          Id == TY_Image ||
118
4
          Id == TY_Object ||
119
0
          Id == TY_LTO_BC);
120
16
}
121
122
49.9k
bool types::isAcceptedByClang(ID Id) {
123
49.9k
  switch (Id) {
124
7.73k
  default:
125
7.73k
    return false;
126
127
59
  case TY_Asm:
128
11.8k
  
case TY_C: 1.18k
case TY_PP_C:
129
11.9k
  case TY_CL:
130
12.4k
  
case TY_CUDA: 11.9k
case TY_PP_CUDA:
131
12.4k
  case TY_CUDA_DEVICE:
132
12.4k
  case TY_HIP:
133
12.8k
  case TY_PP_HIP:
134
12.8k
  case TY_HIP_DEVICE:
135
14.6k
  
case TY_ObjC: 12.9k
case TY_PP_ObjC: 14.6k
case TY_PP_ObjC_Alias:
136
40.3k
  
case TY_CXX: 14.7k
case TY_PP_CXX:
137
41.8k
  
case TY_ObjCXX: 40.4k
case TY_PP_ObjCXX: 41.8k
case TY_PP_ObjCXX_Alias:
138
41.8k
  
case TY_CHeader: 41.8k
case TY_PP_CHeader:
139
41.8k
  case TY_CLHeader:
140
41.8k
  
case TY_ObjCHeader: 41.8k
case TY_PP_ObjCHeader:
141
41.9k
  
case TY_CXXHeader: 41.8k
case TY_PP_CXXHeader:
142
41.9k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
143
41.9k
  
case TY_CXXModule: 41.9k
case TY_PP_CXXModule:
144
41.9k
  
case TY_AST: 41.9k
case TY_ModuleFile: 41.9k
case TY_PCH:
145
42.2k
  
case TY_LLVM_IR: 41.9k
case TY_LLVM_BC:
146
42.2k
    return true;
147
49.9k
  }
148
49.9k
}
149
150
212k
bool types::isObjC(ID Id) {
151
212k
  switch (Id) {
152
194k
  default:
153
194k
    return false;
154
155
8.70k
  
case TY_ObjC: 8.68k
case TY_PP_ObjC: 8.70k
case TY_PP_ObjC_Alias:
156
17.3k
  
case TY_ObjCXX: 17.3k
case TY_PP_ObjCXX:
157
17.3k
  case TY_ObjCHeader: case TY_PP_ObjCHeader:
158
17.4k
  
case TY_ObjCXXHeader: 17.3k
case TY_PP_ObjCXXHeader: 17.3k
case TY_PP_ObjCXX_Alias:
159
17.4k
    return true;
160
212k
  }
161
212k
}
162
163
42.1k
bool types::isOpenCL(ID Id) { return Id == TY_CL; }
164
165
272k
bool types::isCXX(ID Id) {
166
272k
  switch (Id) {
167
95.5k
  default:
168
95.5k
    return false;
169
170
157k
  case TY_CXX: case TY_PP_CXX:
171
170k
  
case TY_ObjCXX: 170k
case TY_PP_ObjCXX: 170k
case TY_PP_ObjCXX_Alias:
172
170k
  case TY_CXXHeader: case TY_PP_CXXHeader:
173
170k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
174
170k
  case TY_CXXModule: case TY_PP_CXXModule:
175
174k
  
case TY_CUDA: 172k
case TY_PP_CUDA: 172k
case TY_CUDA_DEVICE:
176
175k
  case TY_HIP:
177
175k
  case TY_PP_HIP:
178
177k
  case TY_HIP_DEVICE:
179
177k
    return true;
180
272k
  }
181
272k
}
182
183
43.1k
bool types::isLLVMIR(ID Id) {
184
43.1k
  switch (Id) {
185
42.8k
  default:
186
42.8k
    return false;
187
188
57
  case TY_LLVM_IR:
189
292
  case TY_LLVM_BC:
190
292
  case TY_LTO_IR:
191
293
  case TY_LTO_BC:
192
293
    return true;
193
43.1k
  }
194
43.1k
}
195
196
46.1k
bool types::isCuda(ID Id) {
197
46.1k
  switch (Id) {
198
45.8k
  default:
199
45.8k
    return false;
200
201
295
  case TY_CUDA:
202
296
  case TY_PP_CUDA:
203
296
  case TY_CUDA_DEVICE:
204
296
    return true;
205
46.1k
  }
206
46.1k
}
207
208
46.1k
bool types::isHIP(ID Id) {
209
46.1k
  switch (Id) {
210
45.9k
  default:
211
45.9k
    return false;
212
213
186
  case TY_HIP:
214
188
  case TY_PP_HIP:
215
188
  case TY_HIP_DEVICE:
216
188
    return true;
217
46.1k
  }
218
46.1k
}
219
220
20
bool types::isFortran(ID Id) {
221
20
  switch (Id) {
222
3
  default:
223
3
    return false;
224
225
17
  
case TY_Fortran: 2
case TY_PP_Fortran:
226
17
    return true;
227
20
  }
228
20
}
229
230
307
bool types::isSrcFile(ID Id) {
231
307
  return Id != TY_Object && 
getPreprocessedType(Id) != TY_INVALID275
;
232
307
}
233
234
55.5k
types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
235
55.5k
  return llvm::StringSwitch<types::ID>(Ext)
236
55.5k
           .Case("c", TY_C)
237
55.5k
           .Case("C", TY_CXX)
238
55.5k
           .Case("F", TY_Fortran)
239
55.5k
           .Case("f", TY_PP_Fortran)
240
55.5k
           .Case("h", TY_CHeader)
241
55.5k
           .Case("H", TY_CXXHeader)
242
55.5k
           .Case("i", TY_PP_C)
243
55.5k
           .Case("m", TY_ObjC)
244
55.5k
           .Case("M", TY_ObjCXX)
245
55.5k
           .Case("o", TY_Object)
246
55.5k
           .Case("S", TY_Asm)
247
55.5k
           .Case("s", TY_PP_Asm)
248
55.5k
           .Case("bc", TY_LLVM_BC)
249
55.5k
           .Case("cc", TY_CXX)
250
55.5k
           .Case("CC", TY_CXX)
251
55.5k
           .Case("cl", TY_CL)
252
55.5k
           .Case("cp", TY_CXX)
253
55.5k
           .Case("cu", TY_CUDA)
254
55.5k
           .Case("hh", TY_CXXHeader)
255
55.5k
           .Case("ii", TY_PP_CXX)
256
55.5k
           .Case("ll", TY_LLVM_IR)
257
55.5k
           .Case("mi", TY_PP_ObjC)
258
55.5k
           .Case("mm", TY_ObjCXX)
259
55.5k
           .Case("rs", TY_RenderScript)
260
55.5k
           .Case("adb", TY_Ada)
261
55.5k
           .Case("ads", TY_Ada)
262
55.5k
           .Case("asm", TY_PP_Asm)
263
55.5k
           .Case("ast", TY_AST)
264
55.5k
           .Case("ccm", TY_CXXModule)
265
55.5k
           .Case("cpp", TY_CXX)
266
55.5k
           .Case("CPP", TY_CXX)
267
55.5k
           .Case("c++", TY_CXX)
268
55.5k
           .Case("C++", TY_CXX)
269
55.5k
           .Case("cui", TY_PP_CUDA)
270
55.5k
           .Case("cxx", TY_CXX)
271
55.5k
           .Case("CXX", TY_CXX)
272
55.5k
           .Case("F90", TY_Fortran)
273
55.5k
           .Case("f90", TY_PP_Fortran)
274
55.5k
           .Case("F95", TY_Fortran)
275
55.5k
           .Case("f95", TY_PP_Fortran)
276
55.5k
           .Case("for", TY_PP_Fortran)
277
55.5k
           .Case("FOR", TY_PP_Fortran)
278
55.5k
           .Case("fpp", TY_Fortran)
279
55.5k
           .Case("FPP", TY_Fortran)
280
55.5k
           .Case("gch", TY_PCH)
281
55.5k
           .Case("hip", TY_HIP)
282
55.5k
           .Case("hpp", TY_CXXHeader)
283
55.5k
           .Case("hxx", TY_CXXHeader)
284
55.5k
           .Case("iim", TY_PP_CXXModule)
285
55.5k
           .Case("lib", TY_Object)
286
55.5k
           .Case("mii", TY_PP_ObjCXX)
287
55.5k
           .Case("obj", TY_Object)
288
55.5k
           .Case("ifs", TY_IFS)
289
55.5k
           .Case("pch", TY_PCH)
290
55.5k
           .Case("pcm", TY_ModuleFile)
291
55.5k
           .Case("c++m", TY_CXXModule)
292
55.5k
           .Case("cppm", TY_CXXModule)
293
55.5k
           .Case("cxxm", TY_CXXModule)
294
55.5k
           .Default(TY_INVALID);
295
55.5k
}
296
297
4.79k
types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
298
38.9k
  for (unsigned i=0; i<numTypes; 
++i34.1k
) {
299
38.9k
    types::ID Id = (types::ID) (i + 1);
300
38.9k
    if (canTypeBeUserSpecified(Id) &&
301
34.5k
        strcmp(Name, getInfo(Id).Name) == 0)
302
4.79k
      return Id;
303
38.9k
  }
304
  // Accept "cu" as an alias for "cuda" for NVCC compatibility
305
0
  if (strcmp(Name, "cu") == 0) {
306
0
    return types::TY_CUDA;
307
0
  }
308
0
  return TY_INVALID;
309
0
}
310
311
llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
312
138k
types::getCompilationPhases(ID Id, phases::ID LastPhase) {
313
138k
  llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> P;
314
138k
  const auto &Info = getInfo(Id);
315
970k
  for (int I = 0; I <= LastPhase; 
++I831k
)
316
831k
    if (Info.Phases.contains(static_cast<phases::ID>(I)))
317
535k
      P.push_back(static_cast<phases::ID>(I));
318
138k
  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
319
0
  return P;
320
138k
}
321
322
llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
323
types::getCompilationPhases(const clang::driver::Driver &Driver,
324
46.1k
                            llvm::opt::DerivedArgList &DAL, ID Id) {
325
46.1k
  phases::ID LastPhase;
326
327
  // Filter to compiler mode. When the compiler is run as a preprocessor then
328
  // compilation is not an option.
329
  // -S runs the compiler in Assembly listing mode.
330
46.1k
  if (Driver.CCCIsCPP() || 
DAL.getLastArg(options::OPT_E)46.1k
||
331
44.9k
      DAL.getLastArg(options::OPT__SLASH_EP) ||
332
44.9k
      DAL.getLastArg(options::OPT_M, options::OPT_MM) ||
333
44.9k
      DAL.getLastArg(options::OPT__SLASH_P))
334
1.28k
    LastPhase = phases::Preprocess;
335
336
  // --precompile only runs up to precompilation.
337
  // This is a clang extension and is not compatible with GCC.
338
44.8k
  else if (DAL.getLastArg(options::OPT__precompile))
339
8
    LastPhase = phases::Precompile;
340
341
  // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
342
44.8k
  else if (DAL.getLastArg(options::OPT_fsyntax_only) ||
343
16.9k
           DAL.getLastArg(options::OPT_print_supported_cpus) ||
344
16.9k
           DAL.getLastArg(options::OPT_module_file_info) ||
345
16.9k
           DAL.getLastArg(options::OPT_verify_pch) ||
346
16.9k
           DAL.getLastArg(options::OPT_rewrite_objc) ||
347
16.9k
           DAL.getLastArg(options::OPT_rewrite_legacy_objc) ||
348
16.9k
           DAL.getLastArg(options::OPT__migrate) ||
349
16.9k
           DAL.getLastArg(options::OPT__analyze) ||
350
16.8k
           DAL.getLastArg(options::OPT_emit_ast))
351
28.0k
    LastPhase = phases::Compile;
352
353
16.8k
  else if (DAL.getLastArg(options::OPT_S) ||
354
15.5k
           DAL.getLastArg(options::OPT_emit_llvm))
355
1.37k
    LastPhase = phases::Backend;
356
357
15.5k
  else if (DAL.getLastArg(options::OPT_c))
358
7.03k
    LastPhase = phases::Assemble;
359
360
  // Generally means, do every phase until Link.
361
8.46k
  else
362
8.46k
    LastPhase = phases::LastPhase;
363
364
46.1k
  return types::getCompilationPhases(Id, LastPhase);
365
46.1k
}
366
367
5.21k
ID types::lookupCXXTypeForCType(ID Id) {
368
5.21k
  switch (Id) {
369
5.14k
  default:
370
5.14k
    return Id;
371
372
76
  case types::TY_C:
373
76
    return types::TY_CXX;
374
0
  case types::TY_PP_C:
375
0
    return types::TY_PP_CXX;
376
0
  case types::TY_CHeader:
377
0
    return types::TY_CXXHeader;
378
0
  case types::TY_PP_CHeader:
379
0
    return types::TY_PP_CXXHeader;
380
5.21k
  }
381
5.21k
}
382
383
28
ID types::lookupHeaderTypeForSourceType(ID Id) {
384
28
  switch (Id) {
385
0
  default:
386
0
    return Id;
387
388
  // FIXME: Handle preprocessed input types.
389
5
  case types::TY_C:
390
5
    return types::TY_CHeader;
391
22
  case types::TY_CXX:
392
22
  case types::TY_CXXModule:
393
22
    return types::TY_CXXHeader;
394
0
  case types::TY_ObjC:
395
0
    return types::TY_ObjCHeader;
396
1
  case types::TY_ObjCXX:
397
1
    return types::TY_ObjCXXHeader;
398
0
  case types::TY_CL:
399
0
    return types::TY_CLHeader;
400
28
  }
401
28
}