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