@@ -79,16 +79,24 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
79
79
.set_expandIncludeLines (!options.prescanAndReformat ||
80
80
options.expandIncludeLinesInPreprocessedOutput )
81
81
.AddCompilerDirectiveSentinel (" dir$" );
82
- if (options.features .IsEnabled (LanguageFeature::OpenACC)) {
82
+ bool noneOfTheAbove{!options.features .IsEnabled (LanguageFeature::OpenACC) &&
83
+ !options.features .IsEnabled (LanguageFeature::OpenMP) &&
84
+ !options.features .IsEnabled (LanguageFeature::CUDA)};
85
+ if (options.features .IsEnabled (LanguageFeature::OpenACC) ||
86
+ (options.prescanAndReformat && noneOfTheAbove)) {
83
87
prescanner.AddCompilerDirectiveSentinel (" $acc" );
84
88
}
85
- if (options.features .IsEnabled (LanguageFeature::OpenMP)) {
89
+ if (options.features .IsEnabled (LanguageFeature::OpenMP) ||
90
+ (options.prescanAndReformat && noneOfTheAbove)) {
86
91
prescanner.AddCompilerDirectiveSentinel (" $omp" );
87
92
prescanner.AddCompilerDirectiveSentinel (" $" ); // OMP conditional line
88
93
}
89
- if (options.features .IsEnabled (LanguageFeature::CUDA)) {
94
+ if (options.features .IsEnabled (LanguageFeature::CUDA) ||
95
+ (options.prescanAndReformat && noneOfTheAbove)) {
90
96
prescanner.AddCompilerDirectiveSentinel (" $cuf" );
91
97
prescanner.AddCompilerDirectiveSentinel (" @cuf" );
98
+ }
99
+ if (options.features .IsEnabled (LanguageFeature::CUDA)) {
92
100
preprocessor_.Define (" _CUDA" , " 1" );
93
101
}
94
102
ProvenanceRange range{allSources.AddIncludedFile (
@@ -119,11 +127,13 @@ void Parsing::EmitPreprocessedSource(
119
127
int sourceLine{0 };
120
128
int column{1 };
121
129
bool inDirective{false };
130
+ bool ompConditionalLine{false };
122
131
bool inContinuation{false };
123
132
bool lineWasBlankBefore{true };
124
133
const AllSources &allSources{allCooked ().allSources ()};
125
- // All directives that flang support are known to have a length of 3 chars
126
- constexpr int directiveNameLength{3 };
134
+ // All directives that flang supports are known to have a length of 4 chars,
135
+ // except for OpenMP conditional compilation lines (!$).
136
+ constexpr int directiveNameLength{4 };
127
137
// We need to know the current directive in order to provide correct
128
138
// continuation for the directive
129
139
std::string directive;
@@ -133,6 +143,7 @@ void Parsing::EmitPreprocessedSource(
133
143
out << ' \n ' ; // TODO: DOS CR-LF line ending if necessary
134
144
column = 1 ;
135
145
inDirective = false ;
146
+ ompConditionalLine = false ;
136
147
inContinuation = false ;
137
148
lineWasBlankBefore = true ;
138
149
++sourceLine;
@@ -153,16 +164,21 @@ void Parsing::EmitPreprocessedSource(
153
164
return ch;
154
165
}};
155
166
167
+ bool inDirectiveSentinel{false };
156
168
if (ch == ' !' && lineWasBlankBefore) {
157
169
// Other comment markers (C, *, D) in original fixed form source
158
170
// input card column 1 will have been deleted or normalized to !,
159
171
// which signifies a comment (directive) in both source forms.
160
172
inDirective = true ;
161
- }
162
- bool inDirectiveSentinel{
163
- inDirective && directive.size () < directiveNameLength};
164
- if (inDirectiveSentinel && IsLetter (ch)) {
165
- directive += getOriginalChar (ch);
173
+ inDirectiveSentinel = true ;
174
+ } else if (inDirective && !ompConditionalLine &&
175
+ directive.size () < directiveNameLength) {
176
+ if (IsLetter (ch) || ch == ' $' || ch == ' @' ) {
177
+ directive += getOriginalChar (ch);
178
+ inDirectiveSentinel = true ;
179
+ } else if (directive == " $" s) {
180
+ ompConditionalLine = true ;
181
+ }
166
182
}
167
183
168
184
std::optional<SourcePosition> position{provenance
@@ -199,9 +215,16 @@ void Parsing::EmitPreprocessedSource(
199
215
// column limit override option.
200
216
// OpenMP and OpenACC directives' continuations should have the
201
217
// corresponding sentinel at the next line.
202
- const auto continuation{
203
- inDirective ? " &\n !$" + directive + " &" : " &\n &" s};
204
- out << continuation;
218
+ out << " &\n " ;
219
+ if (inDirective) {
220
+ if (ompConditionalLine) {
221
+ out << " !$ &" ;
222
+ } else {
223
+ out << ' !' << directive << ' &' ;
224
+ }
225
+ } else {
226
+ out << " &" ;
227
+ }
205
228
column = 7 ; // start of fixed form source field
206
229
++sourceLine;
207
230
inContinuation = true ;
@@ -212,11 +235,20 @@ void Parsing::EmitPreprocessedSource(
212
235
out << ' ' ;
213
236
}
214
237
}
215
- if (!inContinuation && !inDirectiveSentinel && position &&
216
- position->column <= 72 && ch != ' ' ) {
217
- // Preserve original indentation
218
- for (; column < position->column ; ++column) {
219
- out << ' ' ;
238
+ if (ch != ' ' ) {
239
+ if (ompConditionalLine) {
240
+ // Only digits can stay in the label field
241
+ if (!(ch >= ' 0' && ch <= ' 9' )) {
242
+ for (; column < 7 ; ++column) {
243
+ out << ' ' ;
244
+ }
245
+ }
246
+ } else if (!inContinuation && !inDirectiveSentinel && position &&
247
+ position->column <= 72 ) {
248
+ // Preserve original indentation
249
+ for (; column < position->column ; ++column) {
250
+ out << ' ' ;
251
+ }
220
252
}
221
253
}
222
254
out << getOriginalChar (ch);
0 commit comments