Skip to content

Commit 096471c

Browse files
committed
Improved logic for detection of insertion point
This will handle cases like: Task t( // Line 1 &callbackFunc // Line 2: undeclared identifier ); // Line 3 void callbackFunc() {} // Line 4 since the parsed token are: Task void callbackFunc() the previous logic will insert the prototypes berween line 3 and 4, because callbackFunc is first token that comes after the undeclared identifier. After this patch, the current token position is cached so it can be used if the next token comes after the first undeclared identifier.
1 parent 3b8e2ee commit 096471c

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

main.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
7979

8080
void run(const MatchFinder::MatchResult &match) override {
8181
ASTContext *ctx = match.Context;
82-
SourceManager *sm = &ctx->getSourceManager();
82+
SourceManager &sm = ctx->getSourceManager();
8383

8484
const FunctionDecl *f = match.Nodes.getNodeAs<FunctionDecl>("function_decl");
8585
if (f) {
@@ -102,14 +102,9 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
102102
return;
103103
}
104104

105+
detectInsertionPoint(sm, begin, end);
105106
if (!insertionPointFound) {
106-
insertionPointFound = true;
107-
insertionPoint = begin;
108-
presumedInsertionPoint = sm->getPresumedLoc(begin, true);
109-
if (debugOutput) {
110-
outs() << " !! Insertion point set to ";
111-
outs() << begin.getSpellingLineNumber() << ":" << begin.getSpellingColumnNumber() << "\n";
112-
}
107+
return;
113108
}
114109

115110
if (outputOnlyNeededPrototypes) {
@@ -124,7 +119,7 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
124119
}
125120

126121
// Extract line pragma for prototype insertion
127-
writeLineInfo(sm->getPresumedLoc(loc, true));
122+
writeLineInfo(sm.getPresumedLoc(loc, true));
128123

129124
// Extract prototype from function using the pretty printer
130125
// and stopping at the first open curly brace "{"
@@ -142,17 +137,53 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
142137

143138
const VarDecl *v = match.Nodes.getNodeAs<VarDecl>("var_decl");
144139
if (v) {
145-
auto loc = ctx->getFullLoc(v->getLocStart());
140+
FullSourceLoc loc = ctx->getFullLoc(v->getLocStart());
141+
SourceRange r = v->getSourceRange();
142+
FullSourceLoc begin = ctx->getFullLoc(r.getBegin());
143+
FullSourceLoc end = ctx->getFullLoc(r.getEnd());
144+
146145
if (debugOutput) {
147146
outs() << "Variable " << v->getName() << " declared at ";
148-
outs() << loc.getSpellingLineNumber() << ":" << loc.getSpellingColumnNumber() << "\n";
147+
outs() << loc.getSpellingLineNumber() << ":" << loc.getSpellingColumnNumber();
148+
outs() << " (range " << begin.getSpellingLineNumber() << ":" << begin.getSpellingColumnNumber();
149+
outs() << " to " << end.getSpellingLineNumber() << ":" << end.getSpellingColumnNumber() << ")\n";
149150
}
150151
if (v->getParentFunctionOrMethod()) {
151152
if (debugOutput) {
152153
outs() << " Variable is not top level, ignoring.\n";
153154
}
154155
return;
155156
}
157+
158+
detectInsertionPoint(sm, begin, end);
159+
}
160+
}
161+
162+
void detectInsertionPoint(SourceManager &sm, FullSourceLoc &begin, FullSourceLoc &end) {
163+
if (insertionPointFound)return;
164+
165+
FullSourceLoc first = undeclaredIdentifiers.front()->location;
166+
167+
if (first.isBeforeInTranslationUnitThan(begin)) {
168+
if (debugOutput) {
169+
outs() << " !! Insertion point found!\n";
170+
}
171+
insertionPointFound = true;
172+
return;
173+
}
174+
175+
insertionPoint = begin;
176+
presumedInsertionPoint = sm.getPresumedLoc(begin, true);
177+
if (debugOutput) {
178+
outs() << " Insertion point pushed to ";
179+
outs() << begin.getSpellingLineNumber() << ":" << begin.getSpellingColumnNumber() << "\n";
180+
}
181+
182+
if (first.isBeforeInTranslationUnitThan(end)) {
183+
if (debugOutput) {
184+
outs() << " !! Insertion point found!\n";
185+
}
186+
insertionPointFound = true;
156187
}
157188
}
158189

0 commit comments

Comments
 (0)