@@ -79,7 +79,7 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
79
79
80
80
void run (const MatchFinder::MatchResult &match) override {
81
81
ASTContext *ctx = match.Context ;
82
- SourceManager * sm = & ctx->getSourceManager ();
82
+ SourceManager & sm = ctx->getSourceManager ();
83
83
84
84
const FunctionDecl *f = match.Nodes .getNodeAs <FunctionDecl>(" function_decl" );
85
85
if (f) {
@@ -102,14 +102,9 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
102
102
return ;
103
103
}
104
104
105
+ detectInsertionPoint (sm, begin, end);
105
106
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 ;
113
108
}
114
109
115
110
if (outputOnlyNeededPrototypes) {
@@ -124,7 +119,7 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
124
119
}
125
120
126
121
// Extract line pragma for prototype insertion
127
- writeLineInfo (sm-> getPresumedLoc (loc, true ));
122
+ writeLineInfo (sm. getPresumedLoc (loc, true ));
128
123
129
124
// Extract prototype from function using the pretty printer
130
125
// and stopping at the first open curly brace "{"
@@ -142,17 +137,53 @@ class INOPreprocessorMatcherCallback : public MatchFinder::MatchCallback {
142
137
143
138
const VarDecl *v = match.Nodes .getNodeAs <VarDecl>(" var_decl" );
144
139
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
+
146
145
if (debugOutput) {
147
146
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 " ;
149
150
}
150
151
if (v->getParentFunctionOrMethod ()) {
151
152
if (debugOutput) {
152
153
outs () << " Variable is not top level, ignoring.\n " ;
153
154
}
154
155
return ;
155
156
}
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 ;
156
187
}
157
188
}
158
189
0 commit comments