Coverage Report

Created: 2020-02-25 14:32

/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
  const llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> Phases;
28
};
29
30
static const TypeInfo TypeInfos[] = {
31
#define TYPE(NAME, ID, PP_TYPE, TEMP_SUFFIX, ...) \
32
  { NAME, TEMP_SUFFIX, TY_##PP_TYPE, { __VA_ARGS__ }, },
33
#include "clang/Driver/Types.def"
34
#undef TYPE
35
};
36
static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
37
38
255k
static const TypeInfo &getInfo(unsigned id) {
39
255k
  assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");
40
255k
  return TypeInfos[id - 1];
41
255k
}
42
43
27.4k
const char *types::getTypeName(ID Id) {
44
27.4k
  return getInfo(Id).Name;
45
27.4k
}
46
47
50.3k
types::ID types::getPreprocessedType(ID Id) {
48
50.3k
  ID PPT = getInfo(Id).PreprocessedType;
49
50.3k
  assert((llvm::is_contained(getInfo(Id).Phases, phases::Preprocess) !=
50
50.3k
          (PPT == TY_INVALID)) &&
51
50.3k
         "Unexpected Preprocess Type.");
52
50.3k
  return PPT;
53
50.3k
}
54
55
157
static bool isPrepeocessedModuleType(ID Id) {
56
157
  return Id == TY_CXXModule || Id == TY_PP_CXXModule;
57
157
}
58
59
78
types::ID types::getPrecompiledType(ID Id) {
60
78
  if (isPrepeocessedModuleType(Id))
61
6
    return TY_ModuleFile;
62
72
  if (onlyPrecompileType(Id))
63
72
    return TY_PCH;
64
0
  return TY_INVALID;
65
0
}
66
67
9.25k
const char *types::getTypeTempSuffix(ID Id, bool CLMode) {
68
9.25k
  if (CLMode) {
69
763
    switch (Id) {
70
450
    case TY_Object:
71
450
    case TY_LTO_BC:
72
450
      return "obj";
73
450
    case TY_Image:
74
288
      return "exe";
75
450
    case TY_PP_Asm:
76
16
      return "asm";
77
450
    default:
78
9
      break;
79
8.49k
    }
80
8.49k
  }
81
8.49k
  return getInfo(Id).TempSuffix;
82
8.49k
}
83
84
0
bool types::onlyAssembleType(ID Id) {
85
0
  return llvm::is_contained(getInfo(Id).Phases, phases::Assemble) &&
86
0
         !llvm::is_contained(getInfo(Id).Phases, phases::Compile) &&
87
0
         !llvm::is_contained(getInfo(Id).Phases, phases::Backend);
88
0
}
89
90
79
bool types::onlyPrecompileType(ID Id) {
91
79
  return llvm::is_contained(getInfo(Id).Phases, phases::Precompile) &&
92
79
         !isPrepeocessedModuleType(Id);
93
79
}
94
95
31.3k
bool types::canTypeBeUserSpecified(ID Id) {
96
31.3k
  static const clang::driver::types::ID kStaticLangageTypes[] = {
97
31.3k
      TY_CUDA_DEVICE,   TY_HIP_DEVICE,    TY_PP_CHeader,
98
31.3k
      TY_PP_ObjCHeader, TY_PP_CXXHeader,  TY_PP_ObjCXXHeader,
99
31.3k
      TY_PP_CXXModule,  TY_LTO_IR,        TY_LTO_BC,
100
31.3k
      TY_Plist,         TY_RewrittenObjC, TY_RewrittenLegacyObjC,
101
31.3k
      TY_Remap,         TY_PCH,           TY_Object,
102
31.3k
      TY_Image,         TY_dSYM,          TY_Dependencies,
103
31.3k
      TY_CUDA_FATBIN,   TY_HIP_FATBIN};
104
31.3k
  return !llvm::is_contained(kStaticLangageTypes, Id);
105
31.3k
}
106
107
3.96k
bool types::appendSuffixForType(ID Id) {
108
3.96k
  return Id == TY_PCH || 
Id == TY_dSYM3.96k
||
Id == TY_CUDA_FATBIN3.87k
||
109
3.96k
         
Id == TY_HIP_FATBIN3.86k
;
110
3.96k
}
111
112
14
bool types::canLipoType(ID Id) {
113
14
  return (Id == TY_Nothing ||
114
14
          Id == TY_Image ||
115
14
          
Id == TY_Object4
||
116
14
          
Id == TY_LTO_BC0
);
117
14
}
118
119
33.2k
bool types::isAcceptedByClang(ID Id) {
120
33.2k
  switch (Id) {
121
6.56k
  default:
122
6.56k
    return false;
123
0
124
26.7k
  case TY_Asm:
125
26.7k
  case TY_C: case TY_PP_C:
126
26.7k
  case TY_CL:
127
26.7k
  case TY_CUDA: case TY_PP_CUDA:
128
26.7k
  case TY_CUDA_DEVICE:
129
26.7k
  case TY_HIP:
130
26.7k
  case TY_PP_HIP:
131
26.7k
  case TY_HIP_DEVICE:
132
26.7k
  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
133
26.7k
  case TY_CXX: case TY_PP_CXX:
134
26.7k
  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
135
26.7k
  case TY_CHeader: case TY_PP_CHeader:
136
26.7k
  case TY_CLHeader:
137
26.7k
  case TY_ObjCHeader: case TY_PP_ObjCHeader:
138
26.7k
  case TY_CXXHeader: case TY_PP_CXXHeader:
139
26.7k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
140
26.7k
  case TY_CXXModule: case TY_PP_CXXModule:
141
26.7k
  case TY_AST: case TY_ModuleFile:
142
26.7k
  case TY_LLVM_IR: case TY_LLVM_BC:
143
26.7k
    return true;
144
33.2k
  }
145
33.2k
}
146
147
80.6k
bool types::isObjC(ID Id) {
148
80.6k
  switch (Id) {
149
71.1k
  default:
150
71.1k
    return false;
151
0
152
9.49k
  case TY_ObjC: case TY_PP_ObjC: case TY_PP_ObjC_Alias:
153
9.49k
  case TY_ObjCXX: case TY_PP_ObjCXX:
154
9.49k
  case TY_ObjCHeader: case TY_PP_ObjCHeader:
155
9.49k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader: case TY_PP_ObjCXX_Alias:
156
9.49k
    return true;
157
80.6k
  }
158
80.6k
}
159
160
177k
bool types::isCXX(ID Id) {
161
177k
  switch (Id) {
162
78.8k
  default:
163
78.8k
    return false;
164
0
165
98.3k
  case TY_CXX: case TY_PP_CXX:
166
98.3k
  case TY_ObjCXX: case TY_PP_ObjCXX: case TY_PP_ObjCXX_Alias:
167
98.3k
  case TY_CXXHeader: case TY_PP_CXXHeader:
168
98.3k
  case TY_ObjCXXHeader: case TY_PP_ObjCXXHeader:
169
98.3k
  case TY_CXXModule: case TY_PP_CXXModule:
170
98.3k
  case TY_CUDA: case TY_PP_CUDA: case TY_CUDA_DEVICE:
171
98.3k
  case TY_HIP:
172
98.3k
  case TY_PP_HIP:
173
98.3k
  case TY_HIP_DEVICE:
174
98.3k
    return true;
175
177k
  }
176
177k
}
177
178
856
bool types::isLLVMIR(ID Id) {
179
856
  switch (Id) {
180
831
  default:
181
831
    return false;
182
0
183
25
  case TY_LLVM_IR:
184
25
  case TY_LLVM_BC:
185
25
  case TY_LTO_IR:
186
25
  case TY_LTO_BC:
187
25
    return true;
188
856
  }
189
856
}
190
191
30.3k
bool types::isCuda(ID Id) {
192
30.3k
  switch (Id) {
193
30.1k
  default:
194
30.1k
    return false;
195
0
196
284
  case TY_CUDA:
197
284
  case TY_PP_CUDA:
198
284
  case TY_CUDA_DEVICE:
199
284
    return true;
200
30.3k
  }
201
30.3k
}
202
203
30.3k
bool types::isHIP(ID Id) {
204
30.3k
  switch (Id) {
205
30.3k
  default:
206
30.3k
    return false;
207
0
208
60
  case TY_HIP:
209
60
  case TY_PP_HIP:
210
60
  case TY_HIP_DEVICE:
211
60
    return true;
212
30.3k
  }
213
30.3k
}
214
215
20
bool types::isFortran(ID Id) {
216
20
  switch (Id) {
217
3
  default:
218
3
    return false;
219
0
220
17
  case TY_Fortran: case TY_PP_Fortran:
221
17
    return true;
222
20
  }
223
20
}
224
225
143
bool types::isSrcFile(ID Id) {
226
143
  return Id != TY_Object && 
getPreprocessedType(Id) != TY_INVALID130
;
227
143
}
228
229
40.9k
types::ID types::lookupTypeForExtension(llvm::StringRef Ext) {
230
40.9k
  return llvm::StringSwitch<types::ID>(Ext)
231
40.9k
           .Case("c", TY_C)
232
40.9k
           .Case("C", TY_CXX)
233
40.9k
           .Case("F", TY_Fortran)
234
40.9k
           .Case("f", TY_PP_Fortran)
235
40.9k
           .Case("h", TY_CHeader)
236
40.9k
           .Case("H", TY_CXXHeader)
237
40.9k
           .Case("i", TY_PP_C)
238
40.9k
           .Case("m", TY_ObjC)
239
40.9k
           .Case("M", TY_ObjCXX)
240
40.9k
           .Case("o", TY_Object)
241
40.9k
           .Case("S", TY_Asm)
242
40.9k
           .Case("s", TY_PP_Asm)
243
40.9k
           .Case("bc", TY_LLVM_BC)
244
40.9k
           .Case("cc", TY_CXX)
245
40.9k
           .Case("CC", TY_CXX)
246
40.9k
           .Case("cl", TY_CL)
247
40.9k
           .Case("cp", TY_CXX)
248
40.9k
           .Case("cu", TY_CUDA)
249
40.9k
           .Case("hh", TY_CXXHeader)
250
40.9k
           .Case("ii", TY_PP_CXX)
251
40.9k
           .Case("ll", TY_LLVM_IR)
252
40.9k
           .Case("mi", TY_PP_ObjC)
253
40.9k
           .Case("mm", TY_ObjCXX)
254
40.9k
           .Case("rs", TY_RenderScript)
255
40.9k
           .Case("adb", TY_Ada)
256
40.9k
           .Case("ads", TY_Ada)
257
40.9k
           .Case("asm", TY_PP_Asm)
258
40.9k
           .Case("ast", TY_AST)
259
40.9k
           .Case("ccm", TY_CXXModule)
260
40.9k
           .Case("cpp", TY_CXX)
261
40.9k
           .Case("CPP", TY_CXX)
262
40.9k
           .Case("c++", TY_CXX)
263
40.9k
           .Case("C++", TY_CXX)
264
40.9k
           .Case("cui", TY_PP_CUDA)
265
40.9k
           .Case("cxx", TY_CXX)
266
40.9k
           .Case("CXX", TY_CXX)
267
40.9k
           .Case("F90", TY_Fortran)
268
40.9k
           .Case("f90", TY_PP_Fortran)
269
40.9k
           .Case("F95", TY_Fortran)
270
40.9k
           .Case("f95", TY_PP_Fortran)
271
40.9k
           .Case("for", TY_PP_Fortran)
272
40.9k
           .Case("FOR", TY_PP_Fortran)
273
40.9k
           .Case("fpp", TY_Fortran)
274
40.9k
           .Case("FPP", TY_Fortran)
275
40.9k
           .Case("gch", TY_PCH)
276
40.9k
           .Case("hip", TY_HIP)
277
40.9k
           .Case("hpp", TY_CXXHeader)
278
40.9k
           .Case("iim", TY_PP_CXXModule)
279
40.9k
           .Case("lib", TY_Object)
280
40.9k
           .Case("mii", TY_PP_ObjCXX)
281
40.9k
           .Case("obj", TY_Object)
282
40.9k
           .Case("ifs", TY_IFS)
283
40.9k
           .Case("pch", TY_PCH)
284
40.9k
           .Case("pcm", TY_ModuleFile)
285
40.9k
           .Case("c++m", TY_CXXModule)
286
40.9k
           .Case("cppm", TY_CXXModule)
287
40.9k
           .Case("cxxm", TY_CXXModule)
288
40.9k
           .Default(TY_INVALID);
289
40.9k
}
290
291
2.74k
types::ID types::lookupTypeForTypeSpecifier(const char *Name) {
292
31.1k
  for (unsigned i=0; i<numTypes; 
++i28.3k
) {
293
31.1k
    types::ID Id = (types::ID) (i + 1);
294
31.1k
    if (canTypeBeUserSpecified(Id) &&
295
31.1k
        
strcmp(Name, getInfo(Id).Name) == 027.2k
)
296
2.74k
      return Id;
297
31.1k
  }
298
2.74k
299
2.74k
  
return TY_INVALID0
;
300
2.74k
}
301
302
// FIXME: Why don't we just put this list in the defs file, eh.
303
// FIXME: The list is now in Types.def but for now this function will verify
304
//        the old behavior and a subsequent change will delete most of the body.
305
91.2k
void types::getCompilationPhases(ID Id, llvm::SmallVectorImpl<phases::ID> &P) {
306
91.2k
  P = getInfo(Id).Phases;
307
91.2k
  assert(0 < P.size() && "Not enough phases in list");
308
91.2k
  assert(P.size() <= phases::MaxNumberOfPhases && "Too many phases in list");
309
91.2k
}
310
311
void types::getCompilationPhases(const clang::driver::Driver &Driver,
312
                                 llvm::opt::DerivedArgList &DAL, ID Id,
313
30.4k
                                 llvm::SmallVectorImpl<phases::ID> &P) {
314
30.4k
  llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PhaseList;
315
30.4k
  types::getCompilationPhases(Id, PhaseList);
316
30.4k
317
30.4k
  // Filter to compiler mode. When the compiler is run as a preprocessor then
318
30.4k
  // compilation is not an option.
319
30.4k
  // -S runs the compiler in Assembly listing mode.
320
30.4k
  if (Driver.CCCIsCPP() || 
DAL.getLastArg(options::OPT_E)30.3k
||
321
30.4k
      
DAL.getLastArg(options::OPT__SLASH_EP)29.3k
||
322
30.4k
      
DAL.getLastArg(options::OPT_M, options::OPT_MM)29.3k
||
323
30.4k
      
DAL.getLastArg(options::OPT__SLASH_P)26.6k
)
324
3.82k
    llvm::copy_if(PhaseList, std::back_inserter(P),
325
19.0k
                  [](phases::ID Phase) { return Phase <= phases::Preprocess; });
326
26.5k
327
26.5k
  // --precompile only runs up to precompilation.
328
26.5k
  // This is a clang extension and is not compatible with GCC.
329
26.5k
  else if (DAL.getLastArg(options::OPT__precompile))
330
8
    llvm::copy_if(PhaseList, std::back_inserter(P),
331
32
                  [](phases::ID Phase) { return Phase <= phases::Precompile; });
332
26.5k
333
26.5k
  // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
334
26.5k
  else if (DAL.getLastArg(options::OPT_fsyntax_only) ||
335
26.5k
           
DAL.getLastArg(options::OPT_print_supported_cpus)14.9k
||
336
26.5k
           
DAL.getLastArg(options::OPT_module_file_info)14.9k
||
337
26.5k
           
DAL.getLastArg(options::OPT_verify_pch)14.9k
||
338
26.5k
           
DAL.getLastArg(options::OPT_rewrite_objc)14.9k
||
339
26.5k
           
DAL.getLastArg(options::OPT_rewrite_legacy_objc)14.9k
||
340
26.5k
           
DAL.getLastArg(options::OPT__migrate)14.9k
||
341
26.5k
           
DAL.getLastArg(options::OPT__analyze)14.9k
||
342
26.5k
           
DAL.getLastArg(options::OPT_emit_ast)14.8k
)
343
11.7k
    llvm::copy_if(PhaseList, std::back_inserter(P),
344
58.5k
                  [](phases::ID Phase) { return Phase <= phases::Compile; });
345
14.8k
346
14.8k
  else if (DAL.getLastArg(options::OPT_S) ||
347
14.8k
           
DAL.getLastArg(options::OPT_emit_llvm)13.7k
)
348
1.10k
    llvm::copy_if(PhaseList, std::back_inserter(P),
349
5.49k
                  [](phases::ID Phase) { return Phase <= phases::Backend; });
350
13.7k
351
13.7k
  else if (DAL.getLastArg(options::OPT_c))
352
6.24k
    llvm::copy_if(PhaseList, std::back_inserter(P),
353
30.4k
                  [](phases::ID Phase) { return Phase <= phases::Assemble; });
354
7.49k
355
7.49k
  // Generally means, do every phase until Link.
356
7.49k
  else
357
7.49k
    P = PhaseList;
358
30.4k
}
359
360
6.39k
ID types::lookupCXXTypeForCType(ID Id) {
361
6.39k
  switch (Id) {
362
6.32k
  default:
363
6.32k
    return Id;
364
0
365
62
  case types::TY_C:
366
62
    return types::TY_CXX;
367
0
  case types::TY_PP_C:
368
0
    return types::TY_PP_CXX;
369
0
  case types::TY_CHeader:
370
0
    return types::TY_CXXHeader;
371
0
  case types::TY_PP_CHeader:
372
0
    return types::TY_PP_CXXHeader;
373
6.39k
  }
374
6.39k
}
375
376
26
ID types::lookupHeaderTypeForSourceType(ID Id) {
377
26
  switch (Id) {
378
0
  default:
379
0
    return Id;
380
0
381
0
  // FIXME: Handle preprocessed input types.
382
3
  case types::TY_C:
383
3
    return types::TY_CHeader;
384
22
  case types::TY_CXX:
385
22
  case types::TY_CXXModule:
386
22
    return types::TY_CXXHeader;
387
22
  case types::TY_ObjC:
388
0
    return types::TY_ObjCHeader;
389
22
  case types::TY_ObjCXX:
390
1
    return types::TY_ObjCXXHeader;
391
22
  case types::TY_CL:
392
0
    return types::TY_CLHeader;
393
26
  }
394
26
}