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, AmbiguousStructureConstructor,
57 ContiguousOkForSeqAssociation, ForwardRefExplicitTypeDummy,
58 InaccessibleDeferredOverride, CudaWarpMatchFunction, DoConcurrentOffload,
59 TransferBOZ, Coarray, PointerPassObject)
60
61// Portability and suspicious usage warnings
62ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
63 NonTargetPassedToTarget, PointerToPossibleNoncontiguous,
64 ShortCharacterActual, ShortArrayActual, ImplicitInterfaceActual,
65 PolymorphicTransferArg, PointerComponentTransferArg, TransferSizePresence,
66 F202XAllocatableBreakingChange, OptionalMustBePresent, CommonBlockPadding,
67 LogicalVsCBool, BindCCharLength, ProcDummyArgShapes, ExternalNameConflict,
68 FoldingException, FoldingAvoidsRuntimeCrash, FoldingValueChecks,
69 FoldingFailure, FoldingLimit, Interoperability, CharacterInteroperability,
70 Bounds, Preprocessing, Scanning, OpenAccUsage, ProcPointerCompatibility,
71 VoidMold, KnownBadImplicitInterface, EmptyCase, CaseOverflow, CUDAUsage,
72 IgnoreTKRUsage, ExternalInterfaceMismatch, DefinedOperatorArgs, Final,
73 ZeroDoStep, UnusedForallIndex, OpenMPUsage, DataLength, IgnoredDirective,
74 HomonymousSpecific, HomonymousResult, IgnoredIntrinsicFunctionType,
75 PreviousScalarUse, RedeclaredInaccessibleComponent, ImplicitShared,
76 IndexVarRedefinition, IncompatibleImplicitInterfaces,
77 VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
78 MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
79 CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,
80 NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
81 HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
82 RealConstantWidening, VolatileOrAsynchronousTemporary, UnusedVariable,
83 UsedUndefinedVariable, BadValueInDeadCode)
84
87using LanguageFeatureOrWarning = std::variant<LanguageFeature, UsageWarning>;
88using LanguageControlFlag =
89 std::pair<LanguageFeatureOrWarning, /*shouldEnable=*/bool>;
90
91class LanguageFeatureControl {
92public:
93 LanguageFeatureControl();
94 LanguageFeatureControl(const LanguageFeatureControl &) = default;
95
96 void Enable(LanguageFeature f, bool yes = true) { disable_.set(f, !yes); }
97 void EnableWarning(LanguageFeature f, bool yes = true) {
98 warnLanguage_.set(f, yes);
99 }
100 void EnableWarning(UsageWarning w, bool yes = true) {
101 warnUsage_.set(w, yes);
102 }
103 void EnableWarning(LanguageFeatureOrWarning flag, bool yes = true) {
104 if (std::holds_alternative<LanguageFeature>(flag)) {
105 EnableWarning(std::get<LanguageFeature>(flag), yes);
106 } else {
107 EnableWarning(std::get<UsageWarning>(flag), yes);
108 }
109 }
110 void WarnOnAllNonstandard(bool yes = true);
111 bool IsWarnOnAllNonstandard() const { return warnAllLanguage_; }
112 void WarnOnAllUsage(bool yes = true);
113 bool IsWarnOnAllUsage() const { return warnAllUsage_; }
114 void DisableAllNonstandardWarnings() {
115 warnAllLanguage_ = false;
116 warnLanguage_.clear();
117 }
118 void DisableAllUsageWarnings() {
119 warnAllUsage_ = false;
120 warnUsage_.clear();
121 }
122 void DisableAllWarnings() {
123 disableAllWarnings_ = true;
124 DisableAllNonstandardWarnings();
125 DisableAllUsageWarnings();
126 }
127 bool AreWarningsDisabled() const { return disableAllWarnings_; }
128 bool IsEnabled(LanguageFeature f) const { return !disable_.test(f); }
129 bool ShouldWarn(LanguageFeature f) const { return warnLanguage_.test(f); }
130 bool ShouldWarn(UsageWarning w) const { return warnUsage_.test(w); }
131 // Cli options
132 // Find a warning by its Cli spelling, i.e. '[no-]warning-name'.
133 std::optional<LanguageControlFlag> FindWarning(std::string_view input);
134 // Take a string from the Cli and apply it to the LanguageFeatureControl.
135 // Return true if the option was recognized (and hence applied).
136 bool EnableWarning(std::string_view input);
137 // The add and replace functions are not currently used but are provided
138 // to allow a flexible many-to-one mapping from Cli spellings to enum values.
139 // Taking a string by value because the functions own this string after the
140 // call.
141 void AddAlternativeCliSpelling(LanguageFeature f, std::string input) {
142 cliOptions_.insert({input, {f}});
143 }
144 void AddAlternativeCliSpelling(UsageWarning w, std::string input) {
145 cliOptions_.insert({input, {w}});
146 }
147 void ReplaceCliCanonicalSpelling(LanguageFeature f, std::string input);
148 void ReplaceCliCanonicalSpelling(UsageWarning w, std::string input);
149 std::string_view getDefaultCliSpelling(LanguageFeature f) const {
150 return languageFeatureCliCanonicalSpelling_[EnumToInt(f)];
151 };
152 std::string_view getDefaultCliSpelling(UsageWarning w) const {
153 return usageWarningCliCanonicalSpelling_[EnumToInt(w)];
154 };
155 // Return all spellings of operators names, depending on features enabled
156 std::vector<const char *> GetNames(LogicalOperator) const;
157 std::vector<const char *> GetNames(RelationalOperator) const;
158
159private:
160 // Map from Cli syntax of language features and usage warnings to their enum
161 // values.
162 std::unordered_map<std::string, LanguageFeatureOrWarning> cliOptions_;
163 // These two arrays map the enum values to their cannonical Cli spellings.
164 // Since each of the CanonicalSpelling is a string in the domain of the map
165 // above we just use a view of the string instead of another copy.
166 std::array<std::string, LanguageFeature_enumSize>
167 languageFeatureCliCanonicalSpelling_;
168 std::array<std::string, UsageWarning_enumSize>
169 usageWarningCliCanonicalSpelling_;
170 LanguageFeatures disable_;
171 LanguageFeatures warnLanguage_;
172 bool warnAllLanguage_{false};
173 UsageWarnings warnUsage_;
174 bool warnAllUsage_{false};
175 bool disableAllWarnings_{false};
176};
177} // namespace Fortran::common
178#endif // FORTRAN_SUPPORT_FORTRAN_FEATURES_H_
Definition enum-set.h:28
Definition bit-population-count.h:20