Skip to content

Commit 382849d

Browse files
committed
Do not insert prototypes in the middle of a line.
The insertion point of prototypes may be detected to be in the middle of a line for example in the following sketch: int a; int b=forwardDeclared(); void setup() {} void loop() {} int forwardDeclared() { return 0; } the insertion point is determined to be between `int a;` and `int b=..` since the latter declaration use a forward-declared function to initialize variable. Before this patch this resulted in a stray `#line` directive inserted in the middle of a line: int a; #line 1 ...... <- added prototype line int forwardDeclared(); <- added prototype #line 2..... <- other prototypes.... ...... This patch avoids the situation above by inserting a newline just before the first `#line` directive if the insertion point happens to be in the middle of a line. Fix #7
1 parent 3cb4cb0 commit 382849d

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

main.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,7 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
197197

198198
FullSourceLoc first = undeclaredIdentifiers.back()->location;
199199
if (first.isBeforeInTranslationUnitThan(begin)) {
200-
if (debugOutput) {
201-
outs() << " !! Insertion point found!\n";
202-
}
203-
insertionPointFound = true;
200+
markInsertionPointAsFound();
204201
return;
205202
}
206203

@@ -216,10 +213,22 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
216213
}
217214

218215
if (first.isBeforeInTranslationUnitThan(end)) {
216+
markInsertionPointAsFound();
217+
}
218+
}
219+
220+
void markInsertionPointAsFound() {
221+
if (debugOutput) {
222+
outs() << " !! Insertion point found at ";
223+
outs() << insertionPoint.getSpellingLineNumber() << ":" << insertionPoint.getSpellingColumnNumber() << "\n";
224+
}
225+
insertionPointFound = true;
226+
227+
if (insertionPoint.getSpellingColumnNumber() != 1) {
219228
if (debugOutput) {
220-
outs() << " !! Insertion point found!\n";
229+
outs() << " Insertion point is not at the line beginning -> adding a newline\n";
221230
}
222-
insertionPointFound = true;
231+
rewriter.InsertTextAfter(insertionPoint, "\n");
223232
}
224233
}
225234

0 commit comments

Comments
 (0)