clang 23.0.0git
LangOptions.cpp
Go to the documentation of this file.
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
15#include "clang/Config/config.h"
16#include "llvm/Support/Path.h"
17
18using namespace clang;
19
21#define LANGOPT(Name, Bits, Default, Compatibility, Description) Name = Default;
22#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
23 set##Name(Default);
24#include "clang/Basic/LangOptions.def"
25}
26
28#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
29 if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \
30 Name = Default;
31#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
32 if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \
33 Name = static_cast<unsigned>(Default);
34#include "clang/Basic/LangOptions.def"
35
36 // Reset "benign" options with implied values (Options.td ImpliedBy relations)
37 // rather than their defaults. This avoids unexpected combinations and
38 // invocations that cannot be round-tripped to arguments.
39 // FIXME: we should derive this automatically from ImpliedBy in tablegen.
40 AllowFPReassoc = UnsafeFPMath;
41 NoHonorInfs = FastMath;
42 NoHonorNaNs = FastMath;
43
44 // These options do not affect AST generation.
45 NoSanitizeFiles.clear();
48
49 CurrentModule.clear();
50 IsHeaderFile = false;
51}
52
53bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {
54 for (unsigned i = 0, e = NoBuiltinFuncs.size(); i != e; ++i)
55 if (FuncName == NoBuiltinFuncs[i])
56 return true;
57 return false;
58}
59
61 const int Ver = OpenCLCPlusPlus ? OpenCLCPlusPlusVersion : OpenCLVersion;
62 if (OpenCLCPlusPlus && Ver != 100)
63 return VersionTuple(Ver / 100);
64 return VersionTuple(Ver / 100, (Ver % 100) / 10);
65}
66
68 if (!OpenCLCPlusPlus)
69 return OpenCLVersion;
70 if (OpenCLCPlusPlusVersion == 100)
71 return 200;
72 if (OpenCLCPlusPlusVersion == 202100)
73 return 300;
74 llvm_unreachable("Unknown OpenCL version");
75}
76
78 for (const auto &Entry : MacroPrefixMap)
79 if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
80 break;
81}
82
84 std::string Result;
85 {
86 llvm::raw_string_ostream Out(Result);
87 Out << (OpenCLCPlusPlus ? "C++ for OpenCL" : "OpenCL C") << " version "
88 << getOpenCLVersionTuple().getAsString();
89 }
90 return Result;
91}
92
94 const llvm::Triple &T,
95 std::vector<std::string> &Includes,
97 // Set some properties which depend solely on the input kind; it would be nice
98 // to move these to the language standard, and have the driver resolve the
99 // input kind + language standard.
100 //
101 // FIXME: Perhaps a better model would be for a single source file to have
102 // multiple language standards (C / C++ std, ObjC std, OpenCL std, OpenMP std)
103 // simultaneously active?
104 if (Lang == Language::Asm) {
105 Opts.AsmPreprocessor = 1;
106 } else if (Lang == Language::ObjC || Lang == Language::ObjCXX) {
107 Opts.ObjC = 1;
108 }
109
113 Opts.LangStd = LangStd;
114 Opts.LineComment = Std.hasLineComments();
115 Opts.C99 = Std.isC99();
116 Opts.C11 = Std.isC11();
117 Opts.C17 = Std.isC17();
118 Opts.C23 = Std.isC23();
119 Opts.C2y = Std.isC2y();
120 Opts.CPlusPlus = Std.isCPlusPlus();
121 Opts.CPlusPlus11 = Std.isCPlusPlus11();
122 Opts.CPlusPlus14 = Std.isCPlusPlus14();
123 Opts.CPlusPlus17 = Std.isCPlusPlus17();
124 Opts.CPlusPlus20 = Std.isCPlusPlus20();
125 Opts.CPlusPlus23 = Std.isCPlusPlus23();
126 Opts.CPlusPlus26 = Std.isCPlusPlus26();
127 Opts.GNUMode = Std.isGNUMode();
128 Opts.GNUCVersion = 0;
129 Opts.HexFloats = Std.hasHexFloats();
130 Opts.WChar = Std.isCPlusPlus();
131 Opts.Digraphs = Std.hasDigraphs();
132 Opts.RawStringLiterals = Std.hasRawStringLiterals();
133 Opts.AllowLiteralDigitSeparator = Std.allowLiteralDigitSeparator();
134 Opts.NamedLoops = Std.isC2y();
135
136 Opts.HLSL = Lang == Language::HLSL;
137 if (Opts.HLSL) {
138 if (Opts.IncludeDefaultHeader)
139 Includes.push_back("hlsl.h");
140 // Set maximum matrix dimension to 4 for HLSL
141 Opts.MaxMatrixDimension = 4;
142 }
143
144 // Set OpenCL Version.
145 Opts.OpenCL = Std.isOpenCL();
146 if (LangStd == LangStandard::lang_opencl10)
147 Opts.OpenCLVersion = 100;
148 else if (LangStd == LangStandard::lang_opencl11)
149 Opts.OpenCLVersion = 110;
150 else if (LangStd == LangStandard::lang_opencl12)
151 Opts.OpenCLVersion = 120;
152 else if (LangStd == LangStandard::lang_opencl20)
153 Opts.OpenCLVersion = 200;
154 else if (LangStd == LangStandard::lang_opencl30)
155 Opts.OpenCLVersion = 300;
156 else if (LangStd == LangStandard::lang_openclcpp10)
157 Opts.OpenCLCPlusPlusVersion = 100;
158 else if (LangStd == LangStandard::lang_openclcpp2021)
159 Opts.OpenCLCPlusPlusVersion = 202100;
160 else if (LangStd == LangStandard::lang_hlsl2015)
161 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2015;
162 else if (LangStd == LangStandard::lang_hlsl2016)
163 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2016;
164 else if (LangStd == LangStandard::lang_hlsl2017)
165 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2017;
166 else if (LangStd == LangStandard::lang_hlsl2018)
167 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2018;
168 else if (LangStd == LangStandard::lang_hlsl2021)
169 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2021;
170 else if (LangStd == LangStandard::lang_hlsl202x)
171 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202x;
172 else if (LangStd == LangStandard::lang_hlsl202y)
173 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202y;
174
175 // OpenCL has some additional defaults.
176 if (Opts.OpenCL) {
177 Opts.AltiVec = 0;
178 Opts.ZVector = 0;
179 Opts.setDefaultFPContractMode(LangOptions::FPM_On);
180 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
181 Opts.OpenCLPipes = Opts.getOpenCLCompatibleVersion() == 200;
182 Opts.OpenCLGenericAddressSpace = Opts.getOpenCLCompatibleVersion() == 200;
183
184 // Include default header file for OpenCL.
185 if (Opts.IncludeDefaultHeader) {
186 if (Opts.DeclareOpenCLBuiltins) {
187 // Only include base header file for builtin types and constants.
188 Includes.push_back("opencl-c-base.h");
189 } else {
190 Includes.push_back("opencl-c.h");
191 }
192 }
193 }
194
195 Opts.HIP = Lang == Language::HIP;
196 Opts.CUDA = Lang == Language::CUDA || Opts.HIP;
197 if (Opts.HIP) {
198 // HIP toolchain does not support 'Fast' FPOpFusion in backends since it
199 // fuses multiplication/addition instructions without contract flag from
200 // device library functions in LLVM bitcode, which causes accuracy loss in
201 // certain math functions, e.g. tan(-1e20) becomes -0.933 instead of 0.8446.
202 // For device library functions in bitcode to work, 'Strict' or 'Standard'
203 // FPOpFusion options in backends is needed. Therefore 'fast-honor-pragmas'
204 // FP contract option is used to allow fuse across statements in frontend
205 // whereas respecting contract flag in backend.
206 Opts.setDefaultFPContractMode(LangOptions::FPM_FastHonorPragmas);
207 } else if (Opts.CUDA) {
208 if (T.isSPIRV()) {
209 // Emit OpenCL version metadata in LLVM IR when targeting SPIR-V.
210 Opts.OpenCLVersion = 200;
211 }
212 // Allow fuse across statements disregarding pragmas.
213 Opts.setDefaultFPContractMode(LangOptions::FPM_Fast);
214 }
215
216 // OpenCL, C++ and C23 have bool, true, false keywords.
217 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus || Opts.C23;
218
219 // OpenCL and HLSL have half keyword
220 Opts.Half = Opts.OpenCL || Opts.HLSL;
221
222 Opts.PreserveVec3Type = Opts.HLSL;
223}
224
226 FPOptions result(LO);
227 return result;
228}
229
230FPOptionsOverride FPOptions::getChangesSlow(const FPOptions &Base) const {
231 FPOptions::storage_type OverrideMask = 0;
232#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
233 if (get##NAME() != Base.get##NAME()) \
234 OverrideMask |= NAME##Mask;
235#include "clang/Basic/FPOptions.def"
236 return FPOptionsOverride(*this, OverrideMask);
237}
238
239LLVM_DUMP_METHOD void FPOptions::dump() {
240#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
241 llvm::errs() << "\n " #NAME " " << get##NAME();
242#include "clang/Basic/FPOptions.def"
243 llvm::errs() << "\n";
244}
245
246LLVM_DUMP_METHOD void FPOptionsOverride::dump() {
247#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
248 if (has##NAME##Override()) \
249 llvm::errs() << "\n " #NAME " Override is " << get##NAME##Override();
250#include "clang/Basic/FPOptions.def"
251 llvm::errs() << "\n";
252}
253
254std::optional<uint32_t> LangOptions::getCPlusPlusLangStd() const {
255 if (!CPlusPlus)
256 return std::nullopt;
257
259 if (CPlusPlus26)
260 Std = LangStandard::lang_cxx26;
261 else if (CPlusPlus23)
262 Std = LangStandard::lang_cxx23;
263 else if (CPlusPlus20)
264 Std = LangStandard::lang_cxx20;
265 else if (CPlusPlus17)
266 Std = LangStandard::lang_cxx17;
267 else if (CPlusPlus14)
268 Std = LangStandard::lang_cxx14;
269 else if (CPlusPlus11)
270 Std = LangStandard::lang_cxx11;
271 else
272 Std = LangStandard::lang_cxx98;
273
275}
276
277std::optional<uint32_t> LangOptions::getCLangStd() const {
279 if (C2y)
280 Std = LangStandard::lang_c2y;
281 else if (C23)
282 Std = LangStandard::lang_c23;
283 else if (C17)
284 Std = LangStandard::lang_c17;
285 else if (C11)
286 Std = LangStandard::lang_c11;
287 else if (C99)
288 Std = LangStandard::lang_c99;
289 else if (!GNUMode && Digraphs)
290 Std = LangStandard::lang_c94;
291 else
292 return std::nullopt;
293
295}
Defines the clang::LangOptions interface.
Represents difference between two FPOptions values.
LLVM_DUMP_METHOD void dump()
static FPOptions defaultWithoutTrailingStorage(const LangOptions &LO)
Return the default value of FPOptions that's used when trailing storage isn't required.
uint32_t storage_type
LLVM_DUMP_METHOD void dump()
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
void resetNonModularOptions()
Reset all of the options that are not considered when building a module.
std::vector< std::string > NoBuiltinFuncs
A list of all -fno-builtin-* function names (e.g., memset).
std::vector< std::string > XRayNeverInstrumentFiles
Paths to the XRay "never instrument" files specifying which objects (files, functions,...
bool isNoBuiltinFunc(StringRef Name) const
Is this a libc/libm function that is no longer recognized as a builtin because a -fno-builtin-* optio...
std::string getOpenCLVersionString() const
Return the OpenCL C or C++ for OpenCL language name and version as a string.
bool IsHeaderFile
Indicates whether the front-end is explicitly told that the input is a header file (i....
std::vector< std::string > XRayAlwaysInstrumentFiles
Paths to the XRay "always instrument" files specifying which objects (files, functions,...
VersionTuple getOpenCLVersionTuple() const
Return the OpenCL C or C++ version as a VersionTuple.
static void setLangDefaults(LangOptions &Opts, Language Lang, const llvm::Triple &T, std::vector< std::string > &Includes, LangStandard::Kind LangStd=LangStandard::lang_unspecified)
Set language defaults for the given input language and language standard in the given LangOptions obj...
std::map< std::string, std::string, std::greater< std::string > > MacroPrefixMap
A prefix map for FILE, BASE_FILE and __builtin_FILE().
void remapPathPrefix(SmallVectorImpl< char > &Path) const
Remap path prefix according to -fmacro-prefix-path option.
std::optional< uint32_t > getCPlusPlusLangStd() const
Returns the most applicable C++ standard-compliant language version code.
LangStandard::Kind LangStd
The used language standard.
std::optional< uint32_t > getCLangStd() const
Returns the most applicable C standard-compliant language version code.
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
std::vector< std::string > NoSanitizeFiles
Paths to files specifying which objects (files, functions, variables) should not be instrumented.
std::string CurrentModule
The name of the current module, of which the main source file is a part.
The JSON file list parser is used to communicate input to InstallAPI.
@ CPlusPlus23
@ CPlusPlus20
@ CPlusPlus
@ CPlusPlus11
@ CPlusPlus14
@ CPlusPlus26
@ CPlusPlus17
LangStandard::Kind getDefaultLanguageStandard(clang::Language Lang, const llvm::Triple &T)
Language
The language for the input, used to select and validate the language standard and possible actions.
@ Asm
Assembly: we accept this only so that we can preprocess it.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
LangStandard - Information about the properties of a particular language standard.
bool isCPlusPlus20() const
isCPlusPlus20 - Language is a C++20 variant (or later).
bool isCPlusPlus() const
isCPlusPlus - Language is a C++ variant.
bool hasRawStringLiterals() const
hasRawStringLiterals - Language supports R"()" raw string literals.
bool hasHexFloats() const
hasHexFloats - Language supports hexadecimal float constants.
bool isC11() const
isC11 - Language is a superset of C11.
bool isCPlusPlus17() const
isCPlusPlus17 - Language is a C++17 variant (or later).
bool hasDigraphs() const
hasDigraphs - Language supports digraphs.
bool isCPlusPlus14() const
isCPlusPlus14 - Language is a C++14 variant (or later).
bool isC17() const
isC17 - Language is a superset of C17.
bool isCPlusPlus26() const
isCPlusPlus26 - Language is a post-C++26 variant (or later).
bool isC2y() const
isC2y - Language is a superset of C2y.
bool isCPlusPlus11() const
isCPlusPlus11 - Language is a C++11 variant (or later).
std::optional< uint32_t > getVersion() const
Get the version code for this language standard.
bool isC23() const
isC23 - Language is a superset of C23.
static const LangStandard & getLangStandardForKind(Kind K)
bool isOpenCL() const
isOpenCL - Language is a OpenCL variant.
bool hasLineComments() const
Language supports '//' comments.
bool allowLiteralDigitSeparator() const
allowLiteralDigitSeparator - Language supports literal digit seperator
bool isCPlusPlus23() const
isCPlusPlus23 - Language is a post-C++23 variant (or later).
bool isGNUMode() const
isGNUMode - Language includes GNU extensions.
bool isC99() const
isC99 - Language is a superset of C99.