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 "llvm/Support/Path.h"
16
17using namespace clang;
18
20#define LANGOPT(Name, Bits, Default, Compatibility, Description) Name = Default;
21#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
22 set##Name(Default);
23#include "clang/Basic/LangOptions.def"
24}
25
27#define LANGOPT(Name, Bits, Default, Compatibility, Description) \
28 if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \
29 Name = Default;
30#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description) \
31 if constexpr (CompatibilityKind::Compatibility == CompatibilityKind::Benign) \
32 Name = static_cast<unsigned>(Default);
33#include "clang/Basic/LangOptions.def"
34
35 // Reset "benign" options with implied values (Options.td ImpliedBy relations)
36 // rather than their defaults. This avoids unexpected combinations and
37 // invocations that cannot be round-tripped to arguments.
38 // FIXME: we should derive this automatically from ImpliedBy in tablegen.
39 AllowFPReassoc = UnsafeFPMath;
40 NoHonorInfs = FastMath;
41 NoHonorNaNs = FastMath;
42
43 // These options do not affect AST generation.
44 NoSanitizeFiles.clear();
47
48 CurrentModule.clear();
49 IsHeaderFile = false;
50}
51
52bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {
53 for (unsigned i = 0, e = NoBuiltinFuncs.size(); i != e; ++i)
54 if (FuncName == NoBuiltinFuncs[i])
55 return true;
56 return false;
57}
58
60 const int Ver = OpenCLCPlusPlus ? OpenCLCPlusPlusVersion : OpenCLVersion;
61 if (OpenCLCPlusPlus && Ver != 100)
62 return VersionTuple(Ver / 100);
63 return VersionTuple(Ver / 100, (Ver % 100) / 10);
64}
65
67 if (!OpenCLCPlusPlus)
68 return OpenCLVersion;
69 if (OpenCLCPlusPlusVersion == 100)
70 return 200;
71 if (OpenCLCPlusPlusVersion == 202100)
72 return 300;
73 llvm_unreachable("Unknown OpenCL version");
74}
75
77 for (const auto &Entry : MacroPrefixMap)
78 if (llvm::sys::path::replace_path_prefix(Path, Entry.first, Entry.second))
79 break;
80}
81
83 std::string Result;
84 {
85 llvm::raw_string_ostream Out(Result);
86 Out << (OpenCLCPlusPlus ? "C++ for OpenCL" : "OpenCL C") << " version "
87 << getOpenCLVersionTuple().getAsString();
88 }
89 return Result;
90}
91
93 const llvm::Triple &T,
94 std::vector<std::string> &Includes,
96 // Set some properties which depend solely on the input kind; it would be nice
97 // to move these to the language standard, and have the driver resolve the
98 // input kind + language standard.
99 //
100 // FIXME: Perhaps a better model would be for a single source file to have
101 // multiple language standards (C / C++ std, ObjC std, OpenCL std, OpenMP std)
102 // simultaneously active?
103 if (Lang == Language::Asm) {
104 Opts.AsmPreprocessor = 1;
105 } else if (Lang == Language::ObjC || Lang == Language::ObjCXX) {
106 Opts.ObjC = 1;
107 }
108
112 Opts.LangStd = LangStd;
113 Opts.LineComment = Std.hasLineComments();
114 Opts.C99 = Std.isC99();
115 Opts.C11 = Std.isC11();
116 Opts.C17 = Std.isC17();
117 Opts.C23 = Std.isC23();
118 Opts.C2y = Std.isC2y();
119 Opts.CPlusPlus = Std.isCPlusPlus();
120 Opts.CPlusPlus11 = Std.isCPlusPlus11();
121 Opts.CPlusPlus14 = Std.isCPlusPlus14();
122 Opts.CPlusPlus17 = Std.isCPlusPlus17();
123 Opts.CPlusPlus20 = Std.isCPlusPlus20();
124 Opts.CPlusPlus23 = Std.isCPlusPlus23();
125 Opts.CPlusPlus26 = Std.isCPlusPlus26();
126 Opts.GNUMode = Std.isGNUMode();
127 Opts.GNUCVersion = 0;
128 Opts.HexFloats = Std.hasHexFloats();
129 Opts.WChar = Std.isCPlusPlus();
130 Opts.Digraphs = Std.hasDigraphs();
131 Opts.RawStringLiterals = Std.hasRawStringLiterals();
132 Opts.AllowLiteralDigitSeparator = Std.allowLiteralDigitSeparator();
133 Opts.NamedLoops = Std.isC2y();
134
135 Opts.HLSL = Lang == Language::HLSL;
136 if (Opts.HLSL) {
137 if (Opts.IncludeDefaultHeader)
138 Includes.push_back("hlsl.h");
139 // Set maximum matrix dimension to 4 for HLSL
140 Opts.MaxMatrixDimension = 4;
141 }
142
143 // Set OpenCL Version.
144 Opts.OpenCL = Std.isOpenCL();
145 if (LangStd == LangStandard::lang_opencl10)
146 Opts.OpenCLVersion = 100;
147 else if (LangStd == LangStandard::lang_opencl11)
148 Opts.OpenCLVersion = 110;
149 else if (LangStd == LangStandard::lang_opencl12)
150 Opts.OpenCLVersion = 120;
151 else if (LangStd == LangStandard::lang_opencl20)
152 Opts.OpenCLVersion = 200;
153 else if (LangStd == LangStandard::lang_opencl30)
154 Opts.OpenCLVersion = 300;
155 else if (LangStd == LangStandard::lang_openclcpp10)
156 Opts.OpenCLCPlusPlusVersion = 100;
157 else if (LangStd == LangStandard::lang_openclcpp2021)
158 Opts.OpenCLCPlusPlusVersion = 202100;
159 else if (LangStd == LangStandard::lang_hlsl2015)
160 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2015;
161 else if (LangStd == LangStandard::lang_hlsl2016)
162 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2016;
163 else if (LangStd == LangStandard::lang_hlsl2017)
164 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2017;
165 else if (LangStd == LangStandard::lang_hlsl2018)
166 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2018;
167 else if (LangStd == LangStandard::lang_hlsl2021)
168 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_2021;
169 else if (LangStd == LangStandard::lang_hlsl202x)
170 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202x;
171 else if (LangStd == LangStandard::lang_hlsl202y)
172 Opts.HLSLVersion = (unsigned)LangOptions::HLSL_202y;
173
174 // OpenCL has some additional defaults.
175 if (Opts.OpenCL) {
176 Opts.AltiVec = 0;
177 Opts.ZVector = 0;
178 Opts.setDefaultFPContractMode(LangOptions::FPM_On);
179 Opts.OpenCLCPlusPlus = Opts.CPlusPlus;
180 Opts.OpenCLPipes = Opts.getOpenCLCompatibleVersion() == 200;
181 Opts.OpenCLGenericAddressSpace = Opts.getOpenCLCompatibleVersion() == 200;
182
183 // Include default header file for OpenCL.
184 if (Opts.IncludeDefaultHeader) {
185 if (Opts.DeclareOpenCLBuiltins) {
186 // Only include base header file for builtin types and constants.
187 Includes.push_back("opencl-c-base.h");
188 } else {
189 Includes.push_back("opencl-c.h");
190 }
191 }
192 }
193
194 Opts.HIP = Lang == Language::HIP;
195 Opts.CUDA = Lang == Language::CUDA || Opts.HIP;
196 if (Opts.HIP) {
197 // HIP toolchain does not support 'Fast' FPOpFusion in backends since it
198 // fuses multiplication/addition instructions without contract flag from
199 // device library functions in LLVM bitcode, which causes accuracy loss in
200 // certain math functions, e.g. tan(-1e20) becomes -0.933 instead of 0.8446.
201 // For device library functions in bitcode to work, 'Strict' or 'Standard'
202 // FPOpFusion options in backends is needed. Therefore 'fast-honor-pragmas'
203 // FP contract option is used to allow fuse across statements in frontend
204 // whereas respecting contract flag in backend.
205 Opts.setDefaultFPContractMode(LangOptions::FPM_FastHonorPragmas);
206 } else if (Opts.CUDA) {
207 if (T.isSPIRV()) {
208 // Emit OpenCL version metadata in LLVM IR when targeting SPIR-V.
209 Opts.OpenCLVersion = 200;
210 }
211 // Allow fuse across statements disregarding pragmas.
212 Opts.setDefaultFPContractMode(LangOptions::FPM_Fast);
213 }
214
215 // OpenCL, C++ and C23 have bool, true, false keywords.
216 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus || Opts.C23;
217
218 // OpenCL and HLSL have half keyword
219 Opts.Half = Opts.OpenCL || Opts.HLSL;
220
221 Opts.PreserveVec3Type = Opts.HLSL;
222}
223
225 FPOptions result(LO);
226 return result;
227}
228
229FPOptionsOverride FPOptions::getChangesSlow(const FPOptions &Base) const {
230 FPOptions::storage_type OverrideMask = 0;
231#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
232 if (get##NAME() != Base.get##NAME()) \
233 OverrideMask |= NAME##Mask;
234#include "clang/Basic/FPOptions.def"
235 return FPOptionsOverride(*this, OverrideMask);
236}
237
238LLVM_DUMP_METHOD void FPOptions::dump() {
239#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
240 llvm::errs() << "\n " #NAME " " << get##NAME();
241#include "clang/Basic/FPOptions.def"
242 llvm::errs() << "\n";
243}
244
245LLVM_DUMP_METHOD void FPOptionsOverride::dump() {
246#define FP_OPTION(NAME, TYPE, WIDTH, PREVIOUS) \
247 if (has##NAME##Override()) \
248 llvm::errs() << "\n " #NAME " Override is " << get##NAME##Override();
249#include "clang/Basic/FPOptions.def"
250 llvm::errs() << "\n";
251}
252
253std::optional<uint32_t> LangOptions::getCPlusPlusLangStd() const {
254 if (!CPlusPlus)
255 return std::nullopt;
256
258 if (CPlusPlus26)
259 Std = LangStandard::lang_cxx26;
260 else if (CPlusPlus23)
261 Std = LangStandard::lang_cxx23;
262 else if (CPlusPlus20)
263 Std = LangStandard::lang_cxx20;
264 else if (CPlusPlus17)
265 Std = LangStandard::lang_cxx17;
266 else if (CPlusPlus14)
267 Std = LangStandard::lang_cxx14;
268 else if (CPlusPlus11)
269 Std = LangStandard::lang_cxx11;
270 else
271 Std = LangStandard::lang_cxx98;
272
274}
275
276std::optional<uint32_t> LangOptions::getCLangStd() const {
278 if (C2y)
279 Std = LangStandard::lang_c2y;
280 else if (C23)
281 Std = LangStandard::lang_c23;
282 else if (C17)
283 Std = LangStandard::lang_c17;
284 else if (C11)
285 Std = LangStandard::lang_c11;
286 else if (C99)
287 Std = LangStandard::lang_c99;
288 else if (!GNUMode && Digraphs)
289 Std = LangStandard::lang_c94;
290 else
291 return std::nullopt;
292
294}
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.