LangOptions.h revision 360784
1//===- LangOptions.h - C Language Family Language Options -------*- C++ -*-===//
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/// \file
10/// Defines the clang::LangOptions interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_BASIC_LANGOPTIONS_H
15#define LLVM_CLANG_BASIC_LANGOPTIONS_H
16
17#include "clang/Basic/CommentOptions.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/ObjCRuntime.h"
20#include "clang/Basic/Sanitizers.h"
21#include "clang/Basic/Visibility.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/Triple.h"
24#include <string>
25#include <vector>
26
27namespace clang {
28
29/// Bitfields of LangOptions, split out from LangOptions in order to ensure that
30/// this large collection of bitfields is a trivial class type.
31class LangOptionsBase {
32public:
33  // Define simple language options (with no accessors).
34#define LANGOPT(Name, Bits, Default, Description) unsigned Name : Bits;
35#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)
36#include "clang/Basic/LangOptions.def"
37
38protected:
39  // Define language options of enumeration type. These are private, and will
40  // have accessors (below).
41#define LANGOPT(Name, Bits, Default, Description)
42#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
43  unsigned Name : Bits;
44#include "clang/Basic/LangOptions.def"
45};
46
47/// In the Microsoft ABI, this controls the placement of virtual displacement
48/// members used to implement virtual inheritance.
49enum class MSVtorDispMode { Never, ForVBaseOverride, ForVFTable };
50
51/// Keeps track of the various options that can be
52/// enabled, which controls the dialect of C or C++ that is accepted.
53class LangOptions : public LangOptionsBase {
54public:
55  using Visibility = clang::Visibility;
56
57  enum GCMode { NonGC, GCOnly, HybridGC };
58  enum StackProtectorMode { SSPOff, SSPOn, SSPStrong, SSPReq };
59
60  // Automatic variables live on the stack, and when trivial they're usually
61  // uninitialized because it's undefined behavior to use them without
62  // initializing them.
63  enum class TrivialAutoVarInitKind { Uninitialized, Zero, Pattern };
64
65  enum SignedOverflowBehaviorTy {
66    // Default C standard behavior.
67    SOB_Undefined,
68
69    // -fwrapv
70    SOB_Defined,
71
72    // -ftrapv
73    SOB_Trapping
74  };
75
76  // FIXME: Unify with TUKind.
77  enum CompilingModuleKind {
78    /// Not compiling a module interface at all.
79    CMK_None,
80
81    /// Compiling a module from a module map.
82    CMK_ModuleMap,
83
84    /// Compiling a module from a list of header files.
85    CMK_HeaderModule,
86
87    /// Compiling a C++ modules TS module interface unit.
88    CMK_ModuleInterface,
89  };
90
91  enum PragmaMSPointersToMembersKind {
92    PPTMK_BestCase,
93    PPTMK_FullGeneralitySingleInheritance,
94    PPTMK_FullGeneralityMultipleInheritance,
95    PPTMK_FullGeneralityVirtualInheritance
96  };
97
98  using MSVtorDispMode = clang::MSVtorDispMode;
99
100  enum DefaultCallingConvention {
101    DCC_None,
102    DCC_CDecl,
103    DCC_FastCall,
104    DCC_StdCall,
105    DCC_VectorCall,
106    DCC_RegCall
107  };
108
109  enum AddrSpaceMapMangling { ASMM_Target, ASMM_On, ASMM_Off };
110
111  // Corresponds to _MSC_VER
112  enum MSVCMajorVersion {
113    MSVC2010 = 1600,
114    MSVC2012 = 1700,
115    MSVC2013 = 1800,
116    MSVC2015 = 1900,
117    MSVC2017 = 1910,
118    MSVC2017_5 = 1912,
119    MSVC2017_7 = 1914,
120  };
121
122  /// Clang versions with different platform ABI conformance.
123  enum class ClangABI {
124    /// Attempt to be ABI-compatible with code generated by Clang 3.8.x
125    /// (SVN r257626). This causes <1 x long long> to be passed in an
126    /// integer register instead of an SSE register on x64_64.
127    Ver3_8,
128
129    /// Attempt to be ABI-compatible with code generated by Clang 4.0.x
130    /// (SVN r291814). This causes move operations to be ignored when
131    /// determining whether a class type can be passed or returned directly.
132    Ver4,
133
134    /// Attempt to be ABI-compatible with code generated by Clang 6.0.x
135    /// (SVN r321711). This causes determination of whether a type is
136    /// standard-layout to ignore collisions between empty base classes
137    /// and between base classes and member subobjects, which affects
138    /// whether we reuse base class tail padding in some ABIs.
139    Ver6,
140
141    /// Attempt to be ABI-compatible with code generated by Clang 7.0.x
142    /// (SVN r338536). This causes alignof (C++) and _Alignof (C11) to be
143    /// compatible with __alignof (i.e., return the preferred alignment)
144    /// rather than returning the required alignment.
145    Ver7,
146
147    /// Attempt to be ABI-compatible with code generated by Clang 9.0.x
148    /// (SVN r351319). This causes vectors of __int128 to be passed in memory
149    /// instead of passing in multiple scalar registers on x86_64 on Linux and
150    /// NetBSD.
151    Ver9,
152
153    /// Conform to the underlying platform's C and C++ ABIs as closely
154    /// as we can.
155    Latest
156  };
157
158  enum class CoreFoundationABI {
159    /// No interoperability ABI has been specified
160    Unspecified,
161    /// CoreFoundation does not have any language interoperability
162    Standalone,
163    /// Interoperability with the ObjectiveC runtime
164    ObjectiveC,
165    /// Interoperability with the latest known version of the Swift runtime
166    Swift,
167    /// Interoperability with the Swift 5.0 runtime
168    Swift5_0,
169    /// Interoperability with the Swift 4.2 runtime
170    Swift4_2,
171    /// Interoperability with the Swift 4.1 runtime
172    Swift4_1,
173  };
174
175  enum FPContractModeKind {
176    // Form fused FP ops only where result will not be affected.
177    FPC_Off,
178
179    // Form fused FP ops according to FP_CONTRACT rules.
180    FPC_On,
181
182    // Aggressively fuse FP ops (E.g. FMA).
183    FPC_Fast
184  };
185
186  // TODO: merge FEnvAccessModeKind and FPContractModeKind
187  enum FEnvAccessModeKind {
188    FEA_Off,
189
190    FEA_On
191  };
192
193  // Values of the following enumerations correspond to metadata arguments
194  // specified for constrained floating-point intrinsics:
195  // http://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics.
196
197  /// Possible rounding modes.
198  enum FPRoundingModeKind {
199    /// Rounding to nearest, corresponds to "round.tonearest".
200    FPR_ToNearest,
201    /// Rounding toward -Inf, corresponds to "round.downward".
202    FPR_Downward,
203    /// Rounding toward +Inf, corresponds to "round.upward".
204    FPR_Upward,
205    /// Rounding toward zero, corresponds to "round.towardzero".
206    FPR_TowardZero,
207    /// Is determined by runtime environment, corresponds to "round.dynamic".
208    FPR_Dynamic
209  };
210
211  /// Possible floating point exception behavior.
212  enum FPExceptionModeKind {
213    /// Assume that floating-point exceptions are masked.
214    FPE_Ignore,
215    /// Transformations do not cause new exceptions but may hide some.
216    FPE_MayTrap,
217    /// Strictly preserve the floating-point exception semantics.
218    FPE_Strict
219  };
220
221  enum class LaxVectorConversionKind {
222    /// Permit no implicit vector bitcasts.
223    None,
224    /// Permit vector bitcasts between integer vectors with different numbers
225    /// of elements but the same total bit-width.
226    Integer,
227    /// Permit vector bitcasts between all vectors with the same total
228    /// bit-width.
229    All,
230  };
231
232public:
233  /// Set of enabled sanitizers.
234  SanitizerSet Sanitize;
235
236  /// Paths to blacklist files specifying which objects
237  /// (files, functions, variables) should not be instrumented.
238  std::vector<std::string> SanitizerBlacklistFiles;
239
240  /// Paths to the XRay "always instrument" files specifying which
241  /// objects (files, functions, variables) should be imbued with the XRay
242  /// "always instrument" attribute.
243  /// WARNING: This is a deprecated field and will go away in the future.
244  std::vector<std::string> XRayAlwaysInstrumentFiles;
245
246  /// Paths to the XRay "never instrument" files specifying which
247  /// objects (files, functions, variables) should be imbued with the XRay
248  /// "never instrument" attribute.
249  /// WARNING: This is a deprecated field and will go away in the future.
250  std::vector<std::string> XRayNeverInstrumentFiles;
251
252  /// Paths to the XRay attribute list files, specifying which objects
253  /// (files, functions, variables) should be imbued with the appropriate XRay
254  /// attribute(s).
255  std::vector<std::string> XRayAttrListFiles;
256
257  clang::ObjCRuntime ObjCRuntime;
258
259  CoreFoundationABI CFRuntime = CoreFoundationABI::Unspecified;
260
261  std::string ObjCConstantStringClass;
262
263  /// The name of the handler function to be called when -ftrapv is
264  /// specified.
265  ///
266  /// If none is specified, abort (GCC-compatible behaviour).
267  std::string OverflowHandler;
268
269  /// The module currently being compiled as specified by -fmodule-name.
270  std::string ModuleName;
271
272  /// The name of the current module, of which the main source file
273  /// is a part. If CompilingModule is set, we are compiling the interface
274  /// of this module, otherwise we are compiling an implementation file of
275  /// it. This starts as ModuleName in case -fmodule-name is provided and
276  /// changes during compilation to reflect the current module.
277  std::string CurrentModule;
278
279  /// The names of any features to enable in module 'requires' decls
280  /// in addition to the hard-coded list in Module.cpp and the target features.
281  ///
282  /// This list is sorted.
283  std::vector<std::string> ModuleFeatures;
284
285  /// Options for parsing comments.
286  CommentOptions CommentOpts;
287
288  /// A list of all -fno-builtin-* function names (e.g., memset).
289  std::vector<std::string> NoBuiltinFuncs;
290
291  /// Triples of the OpenMP targets that the host code codegen should
292  /// take into account in order to generate accurate offloading descriptors.
293  std::vector<llvm::Triple> OMPTargetTriples;
294
295  /// Name of the IR file that contains the result of the OpenMP target
296  /// host code generation.
297  std::string OMPHostIRFile;
298
299  /// Indicates whether the front-end is explicitly told that the
300  /// input is a header file (i.e. -x c-header).
301  bool IsHeaderFile = false;
302
303  LangOptions();
304
305  // Define accessors/mutators for language options of enumeration type.
306#define LANGOPT(Name, Bits, Default, Description)
307#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
308  Type get##Name() const { return static_cast<Type>(Name); } \
309  void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
310#include "clang/Basic/LangOptions.def"
311
312  /// Are we compiling a module interface (.cppm or module map)?
313  bool isCompilingModule() const {
314    return getCompilingModule() != CMK_None;
315  }
316
317  /// Do we need to track the owning module for a local declaration?
318  bool trackLocalOwningModule() const {
319    return isCompilingModule() || ModulesLocalVisibility;
320  }
321
322  bool isSignedOverflowDefined() const {
323    return getSignedOverflowBehavior() == SOB_Defined;
324  }
325
326  bool isSubscriptPointerArithmetic() const {
327    return ObjCRuntime.isSubscriptPointerArithmetic() &&
328           !ObjCSubscriptingLegacyRuntime;
329  }
330
331  bool isCompatibleWithMSVC(MSVCMajorVersion MajorVersion) const {
332    return MSCompatibilityVersion >= MajorVersion * 100000U;
333  }
334
335  /// Reset all of the options that are not considered when building a
336  /// module.
337  void resetNonModularOptions();
338
339  /// Is this a libc/libm function that is no longer recognized as a
340  /// builtin because a -fno-builtin-* option has been specified?
341  bool isNoBuiltinFunc(StringRef Name) const;
342
343  /// True if any ObjC types may have non-trivial lifetime qualifiers.
344  bool allowsNonTrivialObjCLifetimeQualifiers() const {
345    return ObjCAutoRefCount || ObjCWeak;
346  }
347
348  bool assumeFunctionsAreConvergent() const {
349    return ConvergentFunctions;
350  }
351
352  /// Return the OpenCL C or C++ version as a VersionTuple.
353  VersionTuple getOpenCLVersionTuple() const;
354};
355
356/// Floating point control options
357class FPOptions {
358public:
359  FPOptions() : fp_contract(LangOptions::FPC_Off),
360                fenv_access(LangOptions::FEA_Off) {}
361
362  // Used for serializing.
363  explicit FPOptions(unsigned I)
364      : fp_contract(static_cast<LangOptions::FPContractModeKind>(I & 3)),
365        fenv_access(static_cast<LangOptions::FEnvAccessModeKind>((I >> 2) & 1))
366        {}
367
368  explicit FPOptions(const LangOptions &LangOpts)
369      : fp_contract(LangOpts.getDefaultFPContractMode()),
370        fenv_access(LangOptions::FEA_Off) {}
371  // FIXME: Use getDefaultFEnvAccessMode() when available.
372
373  bool allowFPContractWithinStatement() const {
374    return fp_contract == LangOptions::FPC_On;
375  }
376
377  bool allowFPContractAcrossStatement() const {
378    return fp_contract == LangOptions::FPC_Fast;
379  }
380
381  void setAllowFPContractWithinStatement() {
382    fp_contract = LangOptions::FPC_On;
383  }
384
385  void setAllowFPContractAcrossStatement() {
386    fp_contract = LangOptions::FPC_Fast;
387  }
388
389  void setDisallowFPContract() { fp_contract = LangOptions::FPC_Off; }
390
391  bool allowFEnvAccess() const {
392    return fenv_access == LangOptions::FEA_On;
393  }
394
395  void setAllowFEnvAccess() {
396    fenv_access = LangOptions::FEA_On;
397  }
398
399  void setDisallowFEnvAccess() { fenv_access = LangOptions::FEA_Off; }
400
401  /// Used to serialize this.
402  unsigned getInt() const { return fp_contract | (fenv_access << 2); }
403
404private:
405  /// Adjust BinaryOperator::FPFeatures to match the total bit-field size
406  /// of these two.
407  unsigned fp_contract : 2;
408  unsigned fenv_access : 1;
409};
410
411/// Describes the kind of translation unit being processed.
412enum TranslationUnitKind {
413  /// The translation unit is a complete translation unit.
414  TU_Complete,
415
416  /// The translation unit is a prefix to a translation unit, and is
417  /// not complete.
418  TU_Prefix,
419
420  /// The translation unit is a module.
421  TU_Module
422};
423
424} // namespace clang
425
426#endif // LLVM_CLANG_BASIC_LANGOPTIONS_H
427