FLANG
Fortran-features.h
1//===-- include/flang/Support/Fortran-features.h ----------------*- 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#ifndef FORTRAN_SUPPORT_FORTRAN_FEATURES_H_
10#define FORTRAN_SUPPORT_FORTRAN_FEATURES_H_
11
12#include "Fortran.h"
13#include "flang/Common/enum-set.h"
14#include <string_view>
15#include <vector>
16
17namespace Fortran::common {
18
19// Non-conforming extensions & legacies
20ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
21 FixedFormContinuationWithColumn1Ampersand, LogicalAbbreviations,
22 XOROperator, PunctuationInNames, OptionalFreeFormSpace, BOZExtensions,
23 EmptyStatement, AlternativeNE, ExecutionPartNamelist, DECStructures,
24 DoubleComplex, Byte, StarKind, ExponentMatchingKindParam, QuadPrecision,
25 SlashInitialization, TripletInArrayConstructor, MissingColons,
26 SignedComplexLiteral, OldStyleParameter, ComplexConstructor, PercentLOC,
27 SignedMultOperand, FileName, Carriagecontrol, Convert, Dispose,
28 IOListLeadingComma, AbbreviatedEditDescriptor, ProgramParentheses,
29 PercentRefAndVal, OmitFunctionDummies, CrayPointer, Hollerith, ArithmeticIF,
30 Assign, AssignedGOTO, Pause, OpenACC, OpenMP, CUDA, CruftAfterAmpersand,
31 ClassicCComments, AdditionalFormats, BigIntLiterals, RealDoControls,
32 EquivalenceNumericWithCharacter, EquivalenceNonDefaultNumeric,
33 EquivalenceSameNonSequence, AdditionalIntrinsics, AnonymousParents,
34 OldLabelDoEndStatements, LogicalIntegerAssignment, EmptySourceFile,
35 ProgramReturn, ImplicitNoneTypeNever, ImplicitNoneTypeAlways,
36 ImplicitNoneExternal, ForwardRefImplicitNone, OpenAccessAppend,
37 BOZAsDefaultInteger, DistinguishableSpecifics, DefaultSave,
38 PointerInSeqType, NonCharacterFormat, SaveMainProgram,
39 SaveBigMainProgramVariables, DistinctArrayConstructorLengths, PPCVector,
40 RelaxedIntentInChecking, ForwardRefImplicitNoneData,
41 NullActualForAllocatable, ActualIntegerConvertedToSmallerKind,
42 HollerithOrCharacterAsBOZ, BindingAsProcedure, StatementFunctionExtensions,
43 UseGenericIntrinsicWhenSpecificDoesntMatch, DataStmtExtensions,
44 RedundantContiguous, RedundantAttribute, InitBlankCommon,
45 EmptyBindCDerivedType, MiscSourceExtensions, AllocateToOtherLength,
46 LongNames, IntrinsicAsSpecific, BenignNameClash, BenignRedundancy,
47 NullMoldAllocatableComponentValue, NopassScalarBase, MiscUseExtensions,
48 ImpliedDoIndexScope, DistinctCommonSizes, OddIndexVariableRestrictions,
49 IndistinguishableSpecifics, SubroutineAndFunctionSpecifics,
50 EmptySequenceType, NonSequenceCrayPointee, BranchIntoConstruct,
51 BadBranchTarget, HollerithPolymorphic, ListDirectedSize,
52 NonBindCInteroperability, CudaManaged, CudaUnified,
53 PolymorphicActualAllocatableOrPointerToMonomorphicDummy, RelaxedPureDummy,
54 UndefinableAsynchronousOrVolatileActual, AutomaticInMainProgram, PrintCptr,
55 SavedLocalInSpecExpr, PrintNamelist, AssumedRankPassedToNonAssumedRank,
56 IgnoreIrrelevantAttributes, Unsigned, ContiguousOkForSeqAssociation,
57 ForwardRefExplicitTypeDummy, InaccessibleDeferredOverride,
58 CudaWarpMatchFunction, DoConcurrentOffload, TransferBOZ, Coarray,
59 PointerPassObject, MultipleIdenticalDATA,
60 DefaultStructConstructorNullPointer, AssumedRankIoItem)
61
62// Portability and suspicious usage warnings
63ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
64 NonTargetPassedToTarget, PointerToPossibleNoncontiguous,
65 ShortCharacterActual, ShortArrayActual, ImplicitInterfaceActual,
66 PolymorphicTransferArg, PointerComponentTransferArg, TransferSizePresence,
67 F202XAllocatableBreakingChange, OptionalMustBePresent, CommonBlockPadding,
68 LogicalVsCBool, BindCCharLength, ProcDummyArgShapes, ExternalNameConflict,
69 FoldingException, FoldingAvoidsRuntimeCrash, FoldingValueChecks,
70 FoldingFailure, FoldingLimit, Interoperability, CharacterInteroperability,
71 Bounds, Preprocessing, Scanning, OpenAccUsage, ProcPointerCompatibility,
72 VoidMold, KnownBadImplicitInterface, EmptyCase, CaseOverflow, CUDAUsage,
73 IgnoreTKRUsage, ExternalInterfaceMismatch, DefinedOperatorArgs, Final,
74 ZeroDoStep, UnusedForallIndex, OpenMPUsage, DataLength, IgnoredDirective,
75 HomonymousSpecific, HomonymousResult, IgnoredIntrinsicFunctionType,
76 PreviousScalarUse, RedeclaredInaccessibleComponent, ImplicitShared,
77 IndexVarRedefinition, IncompatibleImplicitInterfaces,
78 VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
79 MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
80 CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,
81 NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
82 HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
83 RealConstantWidening, VolatileOrAsynchronousTemporary, UnusedVariable,
84 UsedUndefinedVariable, BadValueInDeadCode, AssumedTypeSizeDummy,
85 MisplacedIgnoreTKR, NamelistParameter)
86
89using LanguageFeatureOrWarning = std::variant<LanguageFeature, UsageWarning>;
90using LanguageControlFlag =
91 std::pair<LanguageFeatureOrWarning, /*shouldEnable=*/bool>;
92
93class LanguageFeatureControl {
94public:
95 LanguageFeatureControl();
96 LanguageFeatureControl(const LanguageFeatureControl &) = default;
97
98 void Enable(LanguageFeature f, bool yes = true) { disable_.set(f, !yes); }
99 void EnableWarning(LanguageFeature f, bool yes = true) {
100 warnLanguage_.set(f, yes);
101 }
102 void EnableWarning(UsageWarning w, bool yes = true) {
103 warnUsage_.set(w, yes);
104 }
105 void EnableWarning(LanguageFeatureOrWarning flag, bool yes = true) {
106 if (std::holds_alternative<LanguageFeature>(flag)) {
107 EnableWarning(std::get<LanguageFeature>(flag), yes);
108 } else {
109 EnableWarning(std::get<UsageWarning>(flag), yes);
110 }
111 }
112 void WarnOnAllNonstandard(bool yes = true);
113 bool IsWarnOnAllNonstandard() const { return warnAllLanguage_; }
114 void WarnOnAllUsage(bool yes = true);
115 bool IsWarnOnAllUsage() const { return warnAllUsage_; }
116 void DisableAllNonstandardWarnings() {
117 warnAllLanguage_ = false;
118 warnLanguage_.clear();
119 }
120 void DisableAllUsageWarnings() {
121 warnAllUsage_ = false;
122 warnUsage_.clear();
123 }
124 void DisableAllWarnings() {
125 disableAllWarnings_ = true;
126 DisableAllNonstandardWarnings();
127 DisableAllUsageWarnings();
128 }
129 bool AreWarningsDisabled() const { return disableAllWarnings_; }
130 bool IsEnabled(LanguageFeature f) const { return !disable_.test(f); }
131 bool ShouldWarn(LanguageFeature f) const { return warnLanguage_.test(f); }
132 bool ShouldWarn(UsageWarning w) const { return warnUsage_.test(w); }
133 // Cli options
134 // Find a warning by its Cli spelling, i.e. '[no-]warning-name'.
135 std::optional<LanguageControlFlag> FindWarning(std::string_view input);
136 // Take a string from the Cli and apply it to the LanguageFeatureControl.
137 // Return true if the option was recognized (and hence applied).
138 bool EnableWarning(std::string_view input);
139 // The add and replace functions are not currently used but are provided
140 // to allow a flexible many-to-one mapping from Cli spellings to enum values.
141 // Taking a string by value because the functions own this string after the
142 // call.
143 void AddAlternativeCliSpelling(LanguageFeature f, std::string input) {
144 cliOptions_.insert({input, {f}});
145 }
146 void AddAlternativeCliSpelling(UsageWarning w, std::string input) {
147 cliOptions_.insert({input, {w}});
148 }
149 void ReplaceCliCanonicalSpelling(LanguageFeature f, std::string input);
150 void ReplaceCliCanonicalSpelling(UsageWarning w, std::string input);
151 std::string_view getDefaultCliSpelling(LanguageFeature f) const {
152 return languageFeatureCliCanonicalSpelling_[EnumToInt(f)];
153 };
154 std::string_view getDefaultCliSpelling(UsageWarning w) const {
155 return usageWarningCliCanonicalSpelling_[EnumToInt(w)];
156 };
157 // Return all spellings of operators names, depending on features enabled
158 std::vector<const char *> GetNames(LogicalOperator) const;
159 std::vector<const char *> GetNames(RelationalOperator) const;
160
161private:
162 // Map from Cli syntax of language features and usage warnings to their enum
163 // values.
164 std::unordered_map<std::string, LanguageFeatureOrWarning> cliOptions_;
165 // These two arrays map the enum values to their cannonical Cli spellings.
166 // Since each of the CanonicalSpelling is a string in the domain of the map
167 // above we just use a view of the string instead of another copy.
168 std::array<std::string, LanguageFeature_enumSize>
169 languageFeatureCliCanonicalSpelling_;
170 std::array<std::string, UsageWarning_enumSize>
171 usageWarningCliCanonicalSpelling_;
172 LanguageFeatures disable_;
173 LanguageFeatures warnLanguage_;
174 bool warnAllLanguage_{false};
175 UsageWarnings warnUsage_;
176 bool warnAllUsage_{false};
177 bool disableAllWarnings_{false};
178};
179} // namespace Fortran::common
180#endif // FORTRAN_SUPPORT_FORTRAN_FEATURES_H_
Definition enum-set.h:28
Definition bit-population-count.h:20