Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/LangOptions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LangOptions.cpp - C Language Family Language Options ---------------===//
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 defines the LangOptions class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/LangOptions.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/Support/Path.h"
16
17
using namespace clang;
18
19
11.1M
LangOptions::LangOptions() : LangStd(LangStandard::lang_unspecified) {
20
2.67G
#define LANGOPT(Name, Bits, Default, Description) Name = Default;
21
322M
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) set##Name(Default);
22
11.1M
#include "clang/Basic/LangOptions.def"
23
11.1M
}
24
25
1.87k
void LangOptions::resetNonModularOptions() {
26
1.87k
#define LANGOPT(Name, Bits, Default, Description)
27
102k
#define BENIGN_LANGOPT(Name, Bits, Default, Description) Name = Default;
28
1.87k
#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
29
9.35k
  Name = static_cast<unsigned>(Default);
30
1.87k
#include "clang/Basic/LangOptions.def"
31
32
  // These options do not affect AST generation.
33
1.87k
  NoSanitizeFiles.clear();
34
1.87k
  XRayAlwaysInstrumentFiles.clear();
35
1.87k
  XRayNeverInstrumentFiles.clear();
36
37
1.87k
  CurrentModule.clear();
38
1.87k
  IsHeaderFile = false;
39
1.87k
}
40
41
174k
bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {
42
174k
  for (unsigned i = 0, e = NoBuiltinFuncs.size(); i != e; 
++i400
)
43
438
    if (FuncName.equals(NoBuiltinFuncs[i]))
44
38
      return true;
45
174k
  return false;
46
174k
}
47
48
304
VersionTuple LangOptions::getOpenCLVersionTuple() const {
49
304
  const int Ver = OpenCLCPlusPlus ? 
OpenCLCPlusPlusVersion97
:
OpenCLVersion207
;
50
304
  if (OpenCLCPlusPlus && 
Ver != 10097
)
51
89
    return VersionTuple(Ver / 100);
52
215
  return VersionTuple(Ver / 100, (Ver % 100) / 10);
53
304
}
54
55
354M
unsigned LangOptions::getOpenCLCompatibleVersion() const {
56
354M
  if (!OpenCLCPlusPlus)
57
354M
    return OpenCLVersion;
58
612k
  if (OpenCLCPlusPlusVersion == 100)
59
355k
    return 200;
60
256k
  if (OpenCLCPlusPlusVersion == 202100)
61
257k
    return 300;
62
18.4E
  llvm_unreachable("Unknown OpenCL version");
63
18.4E
}
64
65
530
void LangOptions::remapPathPrefix(SmallVectorImpl<char> &Path) const {
66
530
  for (const auto &Entry : MacroPrefixMap)
67
53
    if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
68
25
      break;
69
530
}
70
71
60
std::string LangOptions::getOpenCLVersionString() const {
72
60
  std::string Result;
73
60
  {
74
60
    llvm::raw_string_ostream Out(Result);
75
60
    Out << (OpenCLCPlusPlus ? 
"C++ for OpenCL"21
:
"OpenCL C"39
) << " version "
76
60
        << getOpenCLVersionTuple().getAsString();
77
60
  }
78
60
  return Result;
79
60
}
80
81
void LangOptions::setLangDefaults(LangOptions &Opts, Language Lang,
82
                                  const llvm::Triple &T,
83
                                  std::vector<std::string> &Includes,
84
140k
                                  LangStandard::Kind LangStd) {
85
  // Set some properties which depend solely on the input kind; it would be nice
86
  // to move these to the language standard, and have the driver resolve the
87
  // input kind + language standard.
88
  //
89
  // FIXME: Perhaps a better model would be for a single source file to have
90
  // multiple language standards (C / C++ std, ObjC std, OpenCL std, OpenMP std)
91
  // simultaneously active?
92
140k
  if (Lang == Language::Asm) {
93
84
    Opts.AsmPreprocessor = 1;
94
140k
  } else if (Lang == Language::ObjC || 
Lang == Language::ObjCXX132k
) {
95
12.9k
    Opts.ObjC = 1;
96
12.9k
  }
97
98
140k
  if (LangStd == LangStandard::lang_unspecified)
99
30.8k
    LangStd = getDefaultLanguageStandard(Lang, T);
100
140k
  const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
101
140k
  Opts.LangStd = LangStd;
102
140k
  Opts.LineComment = Std.hasLineComments();
103
140k
  Opts.C99 = Std.isC99();
104
140k
  Opts.C11 = Std.isC11();
105
140k
  Opts.C17 = Std.isC17();
106
140k
  Opts.C2x = Std.isC2x();
107
140k
  Opts.CPlusPlus = Std.isCPlusPlus();
108
140k
  Opts.CPlusPlus11 = Std.isCPlusPlus11();
109
140k
  Opts.CPlusPlus14 = Std.isCPlusPlus14();
110
140k
  Opts.CPlusPlus17 = Std.isCPlusPlus17();
111
140k
  Opts.CPlusPlus20 = Std.isCPlusPlus20();
112
140k
  Opts.CPlusPlus2b = Std.isCPlusPlus2b();
113
140k
  Opts.GNUMode = Std.isGNUMode();
114
140k
  Opts.GNUCVersion = 0;
115
140k
  Opts.HexFloats = Std.hasHexFloats();
116
140k
  Opts.WChar = Std.isCPlusPlus();
117
140k
  Opts.Digraphs = Std.hasDigraphs();
118
119
140k
  Opts.HLSL = Lang == Language::HLSL;
120
121
  // Set OpenCL Version.
122
140k
  Opts.OpenCL = Std.isOpenCL();
123
140k
  if (LangStd == LangStandard::lang_opencl10)
124
52
    Opts.OpenCLVersion = 100;
125
140k
  else if (LangStd == LangStandard::lang_opencl11)
126
32
    Opts.OpenCLVersion = 110;
127
140k
  else if (LangStd == LangStandard::lang_opencl12)
128
482
    Opts.OpenCLVersion = 120;
129
140k
  else if (LangStd == LangStandard::lang_opencl20)
130
292
    Opts.OpenCLVersion = 200;
131
139k
  else if (LangStd == LangStandard::lang_opencl30)
132
196
    Opts.OpenCLVersion = 300;
133
139k
  else if (LangStd == LangStandard::lang_openclcpp10)
134
208
    Opts.OpenCLCPlusPlusVersion = 100;
135
139k
  else if (LangStd == LangStandard::lang_openclcpp2021)
136
108
    Opts.OpenCLCPlusPlusVersion = 202100;
137
139k
  else if (LangStd == LangStandard::lang_hlsl2015)
138
2
    Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2015;
139
139k
  else if (LangStd == LangStandard::lang_hlsl2016)
140
2
    Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2016;
141
139k
  else if (LangStd == LangStandard::lang_hlsl2017)
142
2
    Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2017;
143
139k
  else if (LangStd == LangStandard::lang_hlsl2018)
144
2
    Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2018;
145
139k
  else if (LangStd == LangStandard::lang_hlsl2021)
146
62
    Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2021;
147
139k
  else if (LangStd == LangStandard::lang_hlsl202x)
148
2
    Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202x;
149
150
  // OpenCL has some additional defaults.
151
140k
  if (Opts.OpenCL) {
152
1.37k
    Opts.AltiVec = 0;
153
1.37k
    Opts.ZVector = 0;
154
1.37k
    Opts.setDefaultFPContractMode(LangOptions::FPM_On);
155
1.37k
    Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
156
1.37k
    Opts.OpenCLPipes = Opts.getOpenCLCompatibleVersion() == 200;
157
1.37k
    Opts.OpenCLGenericAddressSpace = Opts.getOpenCLCompatibleVersion() == 200;
158
159
    // Include default header file for OpenCL.
160
1.37k
    if (Opts.IncludeDefaultHeader) {
161
114
      if (Opts.DeclareOpenCLBuiltins) {
162
        // Only include base header file for builtin types and constants.
163
90
        Includes.push_back("opencl-c-base.h");
164
90
      } else {
165
24
        Includes.push_back("opencl-c.h");
166
24
      }
167
114
    }
168
1.37k
  }
169
170
140k
  Opts.HIP = Lang == Language::HIP;
171
140k
  Opts.CUDA = Lang == Language::CUDA || 
Opts.HIP140k
;
172
140k
  if (Opts.HIP) {
173
    // HIP toolchain does not support 'Fast' FPOpFusion in backends since it
174
    // fuses multiplication/addition instructions without contract flag from
175
    // device library functions in LLVM bitcode, which causes accuracy loss in
176
    // certain math functions, e.g. tan(-1e20) becomes -0.933 instead of 0.8446.
177
    // For device library functions in bitcode to work, 'Strict' or 'Standard'
178
    // FPOpFusion options in backends is needed. Therefore 'fast-honor-pragmas'
179
    // FP contract option is used to allow fuse across statements in frontend
180
    // whereas respecting contract flag in backend.
181
204
    Opts.setDefaultFPContractMode(LangOptions::FPM_FastHonorPragmas);
182
140k
  } else if (Opts.CUDA) {
183
506
    if (T.isSPIRV()) {
184
      // Emit OpenCL version metadata in LLVM IR when targeting SPIR-V.
185
10
      Opts.OpenCLVersion = 200;
186
10
    }
187
    // Allow fuse across statements disregarding pragmas.
188
506
    Opts.setDefaultFPContractMode(LangOptions::FPM_Fast);
189
506
  }
190
191
140k
  Opts.RenderScript = Lang == Language::RenderScript;
192
193
  // OpenCL, C++ and C2x have bool, true, false keywords.
194
140k
  Opts.Bool = Opts.OpenCL || 
Opts.CPlusPlus139k
||
Opts.C2x36.7k
;
195
196
  // OpenCL has half keyword
197
140k
  Opts.Half = Opts.OpenCL;
198
140k
}
199
200
730k
FPOptions FPOptions::defaultWithoutTrailingStorage(const LangOptions &LO) {
201
730k
  FPOptions result(LO);
202
730k
  return result;
203
730k
}
204
205
0
LLVM_DUMP_METHOD void FPOptions::dump() {
206
0
#define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
207
0
  llvm::errs() << "\n " #NAME " " << get##NAME();
208
0
#include "clang/Basic/FPOptions.def"
209
0
  llvm::errs() << "\n";
210
0
}
211
212
0
LLVM_DUMP_METHOD void FPOptionsOverride::dump() {
213
0
#define OPTION(NAME, TYPE, WIDTH, PREVIOUS)                                    \
214
0
  if (has##NAME##Override())                                                   \
215
0
    llvm::errs() << "\n " #NAME " Override is " << get##NAME##Override();
216
0
#include "clang/Basic/FPOptions.def"
217
0
  llvm::errs() << "\n";
218
0
}