Coverage Report

Created: 2020-09-19 12:23

/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
917k
    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
391k
static const TypeInfo &getInfo(unsigned id) {
48
391k
  assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
49
391k
  return TypeInfos[id - 1];
50
391k
}
51
52
43.7k
const char *types::getTypeName(ID Id) {
53
43.7k
  return getInfo(Id).Name;
54
43.7k
}
55
56
82.7k
types::ID types::getPreprocessedType(ID Id) {
57
82.7k
  ID PPT = getInfo(Id).PreprocessedType;
58
82.7k
  assert((getInfo(Id).Phases.contains(phases::Preprocess) !=
59
82.7k
          (PPT == TY_INVALID)) &&
60
82.7k
         "Unexpected Preprocess Type.");
61
82.7k
  return PPT;
62
82.7k
}
63
64
165
static bool isPreprocessedModuleType(ID Id) {
65
165
  return Id == TY_CXXModule || Id == TY_PP_CXXModule;
66
165
}
67
68
82
types::ID types::getPrecompiledType(ID Id) {
69
82
  if (isPreprocessedModuleType(Id))
70
6
    return TY_ModuleFile;
71
76
  if (onlyPrecompileType(Id))
72
76
    return TY_PCH;
73
0
  return TY_INVALID;
74
0
}
75
76
10.2k
const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
77
10.2k
  if (CLMode) {
78
790
    switch (Id) {
79
464
    case TY_Object:
80
464
    case TY_LTO_BC:
81
464
      return "obj";
82
295
    case TY_Image:
83
295
      return "exe";
84
19
    case TY_PP_Asm:
85
19
      return "asm";
86
12
    default:
87
12
      break;
88
9.51k
    }
89
9.51k
  }
90
9.51k
  return getInfo(Id).TempSuffix;
91
9.51k
}
92
93
83
bool types::onlyPrecompileType(ID Id) {
94
83
  return getInfo(Id).Phases.contains(phases::Precompile) &&
95
83
         !isPreprocessedModuleType(Id);
96
83
}
97
98
36.6k
bool types::canTypeBeUserSpecified(ID Id) {
99
36.6k
  static const clang::driver::types::ID kStaticLangageTypes[] = {
100
36.6k
      TY_CUDA_DEVICE,   TY_HIP_DEVICE,    TY_PP_CHeader,
101
36.6k
      TY_PP_ObjCHeader, TY_PP_CXXHeader,  TY_PP_ObjCXXHeader,
102
36.6k
      TY_PP_CXXModule,  TY_LTO_IR,        TY_LTO_BC,
103
36.6k
      TY_Plist,         TY_RewrittenObjC, TY_RewrittenLegacyObjC,
104
36.6k
      TY_Remap,         TY_PCH,           TY_Object,
105
36.6k
      TY_Image,         TY_dSYM,          TY_Dependencies,
106
36.6k
      TY_CUDA_FATBIN,   TY_HIP_FATBIN};
107
36.6k
  return !llvm::is_contained(kStaticLangageTypes, Id);
108
36.6k
}
109
110
4.30k
bool types::appendSuffixForType(ID Id) {
111
4.30k
  return Id == TY_PCH || 
Id == TY_dSYM4.30k
||
Id == TY_CUDA_FATBIN4.22k
||
112
4.21k
         Id == TY_HIP_FATBIN;
113
4.30k
}
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
50.1k
bool types::isAcceptedByClang(ID Id) {
123
50.1k
  switch (Id) {
124
7.20k
  default:
125
7.20k
    return false;
126
0
127
42.9k
  case TY_Asm:
128
42.9k
  case TY_C: case TY_PP_C:
129
42.9k
  case TY_CL:
130
42.9k
  case TY_CUDA: case TY_PP_CUDA:
131
42.9k
  case TY_CUDA_DEVICE:
132
42.9k
  case TY_HIP:
133
42.9k
  case TY_PP_HIP:
134
42.9k
  case TY_HIP_DEVICE:
135
42.9k
  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
136
42.9k
  case TY_CXX: case TY_PP_CXX:
137
42.9k
  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
138
42.9k
  case TY_CHeader: case TY_PP_CHeader:
139
42.9k
  case TY_CLHeader:
140
42.9k
  case TY_ObjCHeader: case TY_PP_ObjCHeader:
141
42.9k
  case TY_CXXHeader: case TY_PP_CXXHeader:
142
42.9k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
143
42.9k
  case TY_CXXModule: case TY_PP_CXXModule:
144
42.9k
  case TY_AST: case TY_ModuleFile: case TY_PCH:
145
42.9k
  case TY_LLVM_IR: case TY_LLVM_BC:
146
42.9k
    return true;
147
50.1k
  }
148
50.1k
}
149
150
172k
bool types::isObjC(ID Id) {
151
172k
  switch (Id) {
152
157k
  default:
153
157k
    return false;
154
0
155
15.3k
  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
156
15.3k
  case TY_ObjCXX: case TY_PP_ObjCXX:
157
15.3k
  case TY_ObjCHeader: case TY_PP_ObjCHeader:
158
15.3k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
159
15.3k
    return true;
160
172k
  }
161
172k
}
162
163
276k
bool types::isCXX(ID Id) {
164
276k
  switch (Id) {
165
98.2k
  default:
166
98.2k
    return false;
167
0
168
178k
  case TY_CXX: case TY_PP_CXX:
169
178k
  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
170
178k
  case TY_CXXHeader: case TY_PP_CXXHeader:
171
178k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
172
178k
  case TY_CXXModule: case TY_PP_CXXModule:
173
178k
  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
174
178k
  case TY_HIP:
175
178k
  case TY_PP_HIP:
176
178k
  case TY_HIP_DEVICE:
177
178k
    return true;
178
276k
  }
179
276k
}
180
181
885
bool types::isLLVMIR(ID Id) {
182
885
  switch (Id) {
183
858
  default:
184
858
    return false;
185
0
186
27
  case TY_LLVM_IR:
187
27
  case TY_LLVM_BC:
188
27
  case TY_LTO_IR:
189
27
  case TY_LTO_BC:
190
27
    return true;
191
885
  }
192
885
}
193
194
46.7k
bool types::isCuda(ID Id) {
195
46.7k
  switch (Id) {
196
46.4k
  default:
197
46.4k
    return false;
198
0
199
292
  case TY_CUDA:
200
292
  case TY_PP_CUDA:
201
292
  case TY_CUDA_DEVICE:
202
292
    return true;
203
46.7k
  }
204
46.7k
}
205
206
46.7k
bool types::isHIP(ID Id) {
207
46.7k
  switch (Id) {
208
46.6k
  default:
209
46.6k
    return false;
210
0
211
125
  case TY_HIP:
212
125
  case TY_PP_HIP:
213
125
  case TY_HIP_DEVICE:
214
125
    return true;
215
46.7k
  }
216
46.7k
}
217
218
20
bool types::isFortran(ID Id) {
219
20
  switch (Id) {
220
3
  default:
221
3
    return false;
222
0
223
17
  case TY_Fortran: case TY_PP_Fortran:
224
17
    return true;
225
20
  }
226
20
}
227
228
237
bool types::isSrcFile(ID Id) {
229
237
  return Id != TY_Object && 
getPreprocessedType(Id) != TY_INVALID205
;
230
237
}
231
232
56.0k
types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
233
56.0k
  return llvm::StringSwitch<types::ID>(Ext)
234
56.0k
           .Case("c", TY_C)
235
56.0k
           .Case("C", TY_CXX)
236
56.0k
           .Case("F", TY_Fortran)
237
56.0k
           .Case("f", TY_PP_Fortran)
238
56.0k
           .Case("h", TY_CHeader)
239
56.0k
           .Case("H", TY_CXXHeader)
240
56.0k
           .Case("i", TY_PP_C)
241
56.0k
           .Case("m", TY_ObjC)
242
56.0k
           .Case("M", TY_ObjCXX)
243
56.0k
           .Case("o", TY_Object)
244
56.0k
           .Case("S", TY_Asm)
245
56.0k
           .Case("s", TY_PP_Asm)
246
56.0k
           .Case("bc", TY_LLVM_BC)
247
56.0k
           .Case("cc", TY_CXX)
248
56.0k
           .Case("CC", TY_CXX)
249
56.0k
           .Case("cl", TY_CL)
250
56.0k
           .Case("cp", TY_CXX)
251
56.0k
           .Case("cu", TY_CUDA)
252
56.0k
           .Case("hh", TY_CXXHeader)
253
56.0k
           .Case("ii", TY_PP_CXX)
254
56.0k
           .Case("ll", TY_LLVM_IR)
255
56.0k
           .Case("mi", TY_PP_ObjC)
256
56.0k
           .Case("mm", TY_ObjCXX)
257
56.0k
           .Case("rs", TY_RenderScript)
258
56.0k
           .Case("adb", TY_Ada)
259
56.0k
           .Case("ads", TY_Ada)
260
56.0k
           .Case("asm", TY_PP_Asm)
261
56.0k
           .Case("ast", TY_AST)
262
56.0k
           .Case("ccm", TY_CXXModule)
263
56.0k
           .Case("cpp", TY_CXX)
264
56.0k
           .Case("CPP", TY_CXX)
265
56.0k
           .Case("c++", TY_CXX)
266
56.0k
           .Case("C++", TY_CXX)
267
56.0k
           .Case("cui", TY_PP_CUDA)
268
56.0k
           .Case("cxx", TY_CXX)
269
56.0k
           .Case("CXX", TY_CXX)
270
56.0k
           .Case("F90", TY_Fortran)
271
56.0k
           .Case("f90", TY_PP_Fortran)
272
56.0k
           .Case("F95", TY_Fortran)
273
56.0k
           .Case("f95", TY_PP_Fortran)
274
56.0k
           .Case("for", TY_PP_Fortran)
275
56.0k
           .Case("FOR", TY_PP_Fortran)
276
56.0k
           .Case("fpp", TY_Fortran)
277
56.0k
           .Case("FPP", TY_Fortran)
278
56.0k
           .Case("gch", TY_PCH)
279
56.0k
           .Case("hip", TY_HIP)
280
56.0k
           .Case("hpp", TY_CXXHeader)
281
56.0k
           .Case("hxx", TY_CXXHeader)
282
56.0k
           .Case("iim", TY_PP_CXXModule)
283
56.0k
           .Case("lib", TY_Object)
284
56.0k
           .Case("mii", TY_PP_ObjCXX)
285
56.0k
           .Case("obj", TY_Object)
286
56.0k
           .Case("ifs", TY_IFS)
287
56.0k
           .Case("pch", TY_PCH)
288
56.0k
           .Case("pcm", TY_ModuleFile)
289
56.0k
           .Case("c++m", TY_CXXModule)
290
56.0k
           .Case("cppm", TY_CXXModule)
291
56.0k
           .Case("cxxm", TY_CXXModule)
292
56.0k
           .Default(TY_INVALID);
293
56.0k
}
294
295
4.53k
types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
296
36.3k
  for (unsigned i=0; i<numTypes; 
++i31.8k
) {
297
36.3k
    types::ID Id = (types::ID) (i + 1);
298
36.3k
    if (canTypeBeUserSpecified(Id) &&
299
32.2k
        strcmp(Name, getInfo(Id).Name) == 0)
300
4.53k
      return Id;
301
36.3k
  }
302
  // Accept "cu" as an alias for "cuda" for NVCC compatibility
303
0
  if (strcmp(Name, "cu") == 0) {
304
0
    return types::TY_CUDA;
305
0
  }
306
0
  return TY_INVALID;
307
0
}
308
309
llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
310
140k
types::getCompilationPhases(ID Id, phases::ID LastPhase) {
311
140k
  llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> P;
312
140k
  const auto &Info = getInfo(Id);
313
974k
  for (int I = 0; I <= LastPhase; 
++I834k
)
314
834k
    if (Info.Phases.contains(static_cast<phases::ID>(I)))
315
540k
      P.push_back(static_cast<phases::ID>(I));
316
140k
  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
317
140k
  return P;
318
140k
}
319
320
llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases>
321
types::getCompilationPhases(const clang::driver::Driver &Driver,
322
46.8k
                            llvm::opt::DerivedArgList &DAL, ID Id) {
323
46.8k
  phases::ID LastPhase;
324
46.8k
325
  // Filter to compiler mode. When the compiler is run as a preprocessor then
326
  // compilation is not an option.
327
  // -S runs the compiler in Assembly listing mode.
328
46.8k
  if (Driver.CCCIsCPP() || 
DAL.getLastArg(options::OPT_E)46.7k
||
329
45.6k
      DAL.getLastArg(options::OPT__SLASH_EP) ||
330
45.6k
      DAL.getLastArg(options::OPT_M, options::OPT_MM) ||
331
42.8k
      DAL.getLastArg(options::OPT__SLASH_P))
332
4.01k
    LastPhase = phases::Preprocess;
333
42.7k
334
  // --precompile only runs up to precompilation.
335
  // This is a clang extension and is not compatible with GCC.
336
42.7k
  else if (DAL.getLastArg(options::OPT__precompile))
337
8
    LastPhase = phases::Precompile;
338
42.7k
339
  // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
340
42.7k
  else if (DAL.getLastArg(options::OPT_fsyntax_only) ||
341
16.0k
           DAL.getLastArg(options::OPT_print_supported_cpus) ||
342
16.0k
           DAL.getLastArg(options::OPT_module_file_info) ||
343
16.0k
           DAL.getLastArg(options::OPT_verify_pch) ||
344
16.0k
           DAL.getLastArg(options::OPT_rewrite_objc) ||
345
16.0k
           DAL.getLastArg(options::OPT_rewrite_legacy_objc) ||
346
16.0k
           DAL.getLastArg(options::OPT__migrate) ||
347
16.0k
           DAL.getLastArg(options::OPT__analyze) ||
348
16.0k
           DAL.getLastArg(options::OPT_emit_ast))
349
26.7k
    LastPhase = phases::Compile;
350
15.9k
351
15.9k
  else if (DAL.getLastArg(options::OPT_S) ||
352
14.7k
           DAL.getLastArg(options::OPT_emit_llvm))
353
1.23k
    LastPhase = phases::Backend;
354
14.7k
355
14.7k
  else if (DAL.getLastArg(options::OPT_c))
356
6.65k
    LastPhase = phases::Assemble;
357
8.11k
358
  // Generally means, do every phase until Link.
359
8.11k
  else
360
8.11k
    LastPhase = phases::LastPhase;
361
46.8k
362
46.8k
  return types::getCompilationPhases(Id, LastPhase);
363
46.8k
}
364
365
6.73k
ID types::lookupCXXTypeForCType(ID Id) {
366
6.73k
  switch (Id) {
367
6.66k
  default:
368
6.66k
    return Id;
369
0
370
73
  case types::TY_C:
371
73
    return types::TY_CXX;
372
0
  case types::TY_PP_C:
373
0
    return types::TY_PP_CXX;
374
0
  case types::TY_CHeader:
375
0
    return types::TY_CXXHeader;
376
0
  case types::TY_PP_CHeader:
377
0
    return types::TY_PP_CXXHeader;
378
6.73k
  }
379
6.73k
}
380
381
26
ID types::lookupHeaderTypeForSourceType(ID Id) {
382
26
  switch (Id) {
383
0
  default:
384
0
    return Id;
385
0
386
  // FIXME: Handle preprocessed input types.
387
3
  case types::TY_C:
388
3
    return types::TY_CHeader;
389
22
  case types::TY_CXX:
390
22
  case types::TY_CXXModule:
391
22
    return types::TY_CXXHeader;
392
0
  case types::TY_ObjC:
393
0
    return types::TY_ObjCHeader;
394
1
  case types::TY_ObjCXX:
395
1
    return types::TY_ObjCXXHeader;
396
0
  case types::TY_CL:
397
0
    return types::TY_CLHeader;
398
26
  }
399
26
}