Lines 45-55
Link Here
|
45 |
import java.util.Collections; |
45 |
import java.util.Collections; |
46 |
import java.util.Comparator; |
46 |
import java.util.Comparator; |
47 |
import java.util.List; |
47 |
import java.util.List; |
|
|
48 |
import org.netbeans.modules.cnd.api.model.CsmFile; |
48 |
import org.netbeans.modules.cnd.api.model.CsmMacro; |
49 |
import org.netbeans.modules.cnd.api.model.CsmMacro; |
|
|
50 |
import org.netbeans.modules.cnd.api.model.CsmModelAccessor; |
51 |
import org.netbeans.modules.cnd.api.model.CsmOffsetable; |
49 |
import org.netbeans.modules.cnd.api.model.xref.CsmReference; |
52 |
import org.netbeans.modules.cnd.api.model.xref.CsmReference; |
50 |
import org.netbeans.modules.cnd.apt.support.ClankDriver; |
53 |
import org.netbeans.modules.cnd.apt.support.ClankDriver; |
|
|
54 |
import org.netbeans.modules.cnd.apt.support.ResolvedPath; |
55 |
import org.netbeans.modules.cnd.apt.support.api.PreprocHandler; |
56 |
import org.netbeans.modules.cnd.modelimpl.content.file.FileContent; |
57 |
import org.netbeans.modules.cnd.modelimpl.csm.IncludeImpl; |
51 |
import org.netbeans.modules.cnd.modelimpl.csm.MacroImpl; |
58 |
import org.netbeans.modules.cnd.modelimpl.csm.MacroImpl; |
|
|
59 |
import org.netbeans.modules.cnd.modelimpl.csm.core.ErrorDirectiveImpl; |
52 |
import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl; |
60 |
import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl; |
|
|
61 |
import org.netbeans.modules.cnd.utils.CndUtils; |
53 |
|
62 |
|
54 |
/** |
63 |
/** |
55 |
* Misc static methods used for processing of macros |
64 |
* Misc static methods used for processing of macros |
Lines 60-72
Link Here
|
60 |
private ClankMacroUsagesSupport() { |
69 |
private ClankMacroUsagesSupport() { |
61 |
} |
70 |
} |
62 |
|
71 |
|
|
|
72 |
public static void addPreprocessorDirectives(FileImpl curFile, FileContent parsingFileContent, ClankDriver.APTTokenStreamCache cache) { |
73 |
assert parsingFileContent != null; |
74 |
assert curFile != null; |
75 |
assert cache != null; |
76 |
for (ClankDriver.ClankPreprocessorDirective cur : cache.getPreprocessorDirectives()) { |
77 |
if (cur instanceof ClankDriver.ClankInclusionDirective) { |
78 |
ClankMacroUsagesSupport.addInclude(curFile, parsingFileContent, (ClankDriver.ClankInclusionDirective)cur); |
79 |
} else if (cur instanceof ClankDriver.ClankErrorDirective) { |
80 |
ClankMacroUsagesSupport.addError(curFile, parsingFileContent, (ClankDriver.ClankErrorDirective)cur); |
81 |
} else if (cur instanceof ClankDriver.ClankMacroDirective) { |
82 |
ClankMacroUsagesSupport.addMacro(curFile, parsingFileContent, (ClankDriver.ClankMacroDirective)cur); |
83 |
} else { |
84 |
CndUtils.assertTrueInConsole(false, "unknown directive " + cur.getClass().getSimpleName() + " " + cur); |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
public static void setFileGuard(FileImpl curFile, FileContent parsingFileContent, ClankDriver.APTTokenStreamCache cache) { |
90 |
ClankDriver.FileGuard fileGuard = cache.getFileGuard(); |
91 |
if (fileGuard != null) { |
92 |
curFile.setFileGuard(fileGuard.getStartOfset(), fileGuard.getEndOfset()); |
93 |
} else { |
94 |
curFile.setFileGuard(-1, -1); |
95 |
} |
96 |
} |
97 |
|
98 |
public static void addMacroExpansions(FileImpl curFile, FileContent parsingFileContent, FileImpl startFile, ClankDriver.APTTokenStreamCache cache) { |
99 |
for (ClankDriver.MacroExpansion cur : cache.getMacroExpansions()) { |
100 |
ClankDriver.ClankMacroDirective directive = cur.getReferencedMacro(); |
101 |
if (directive != null) { |
102 |
MacroReference macroRef = MacroReference.createMacroReference(curFile, cur.getStartOfset(), cur.getStartOfset() + cur.getMacroNameLength(), startFile, directive); |
103 |
if (macroRef == null) { |
104 |
if (!curFile.isValid()) { |
105 |
break; |
106 |
} |
107 |
} else { |
108 |
addMacroUsage(curFile, parsingFileContent, macroRef); |
109 |
} |
110 |
} else { |
111 |
// TODO: process invalid macro definition |
112 |
assert false : "Not found referenced ClankMacroDirective " + cur; |
113 |
} |
114 |
} |
115 |
for (ClankDriver.MacroUsage cur : cache.getMacroUsages()) { |
116 |
ClankDriver.ClankMacroDirective directive = cur.getReferencedMacro(); |
117 |
if (directive != null) { |
118 |
MacroReference macroRef = MacroReference.createMacroReference(curFile, cur.getStartOfset(), cur.getEndOfset(), startFile, directive); |
119 |
if (macroRef == null) { |
120 |
if (!curFile.isValid()) { |
121 |
break; |
122 |
} |
123 |
} else { |
124 |
addMacroUsage(curFile, parsingFileContent, macroRef); |
125 |
} |
126 |
} else { |
127 |
// TODO: process invalid macro definition |
128 |
assert false : "Not found referenced ClankMacroDirective " + cur; |
129 |
} |
130 |
} |
131 |
} |
132 |
|
133 |
private static void addMacroUsage(FileImpl curFile, FileContent parsingFileContent, MacroReference macroReference) { |
134 |
parsingFileContent.addReference(macroReference, macroReference.getReferencedObject()); |
135 |
} |
136 |
|
137 |
private static void addMacro(FileImpl curFile, FileContent parsingFileContent, ClankDriver.ClankMacroDirective ppDirective) { |
138 |
if (!ppDirective.isDefined()) { |
139 |
// only #define are handled by old model, not #undef |
140 |
return; |
141 |
} |
142 |
CsmMacro.Kind kind = CsmMacro.Kind.DEFINED; |
143 |
List<CharSequence> params = ppDirective.getParameters(); |
144 |
CharSequence name = ppDirective.getMacroName(); |
145 |
String body = ""; |
146 |
int startOffset = ppDirective.getDirectiveStartOffset(); |
147 |
int endOffset = ppDirective.getDirectiveEndOffset(); |
148 |
int macroNameOffset = ppDirective.getMacroNameOffset(); |
149 |
CsmMacro impl = MacroImpl.create(name, params, body/*sb.toString()*/, curFile, startOffset, endOffset, kind); |
150 |
parsingFileContent.addMacro(impl); |
151 |
parsingFileContent.addReference(new MacroDeclarationReference(curFile, impl, macroNameOffset), impl); |
152 |
} |
153 |
|
63 |
public static List<CsmReference> getMacroUsages(FileImpl fileImpl, FileImpl startFile, ClankDriver.ClankFileInfo foundFileInfo) { |
154 |
public static List<CsmReference> getMacroUsages(FileImpl fileImpl, FileImpl startFile, ClankDriver.ClankFileInfo foundFileInfo) { |
64 |
if (foundFileInfo == null) { |
155 |
if (foundFileInfo == null) { |
65 |
return Collections.emptyList(); |
156 |
return Collections.emptyList(); |
66 |
} |
157 |
} |
67 |
List<CsmReference> res = new ArrayList<>(); |
158 |
List<CsmReference> res = new ArrayList<>(); |
68 |
ClankMacroUsagesSupport.addPreprocessorDirectives(fileImpl, res, foundFileInfo); |
159 |
addPreprocessorDirectives(fileImpl, res, foundFileInfo); |
69 |
ClankMacroUsagesSupport.addMacroExpansions(fileImpl, res, startFile, foundFileInfo); |
160 |
addMacroExpansions(fileImpl, res, startFile, foundFileInfo); |
70 |
Collections.sort(res, new Comparator<CsmReference>() { |
161 |
Collections.sort(res, new Comparator<CsmReference>() { |
71 |
@Override |
162 |
@Override |
72 |
public int compare(CsmReference o1, CsmReference o2) { |
163 |
public int compare(CsmReference o1, CsmReference o2) { |
Lines 76-82
Link Here
|
76 |
return res; |
167 |
return res; |
77 |
} |
168 |
} |
78 |
|
169 |
|
79 |
public static void addPreprocessorDirectives(FileImpl curFile, List<CsmReference> res, ClankDriver.ClankFileInfo cache) { |
170 |
private static void addPreprocessorDirectives(FileImpl curFile, List<CsmReference> res, ClankDriver.ClankFileInfo cache) { |
80 |
assert res != null; |
171 |
assert res != null; |
81 |
assert curFile != null; |
172 |
assert curFile != null; |
82 |
assert cache != null; |
173 |
assert cache != null; |
Lines 87-93
Link Here
|
87 |
} |
178 |
} |
88 |
} |
179 |
} |
89 |
|
180 |
|
90 |
public static void addMacroExpansions(FileImpl curFile, List<CsmReference> res, FileImpl startFile, ClankDriver.ClankFileInfo cache) { |
181 |
private static void addMacroExpansions(FileImpl curFile, List<CsmReference> res, FileImpl startFile, ClankDriver.ClankFileInfo cache) { |
91 |
for (ClankDriver.MacroExpansion cur : cache.getMacroExpansions()) { |
182 |
for (ClankDriver.MacroExpansion cur : cache.getMacroExpansions()) { |
92 |
ClankDriver.ClankMacroDirective directive = cur.getReferencedMacro(); |
183 |
ClankDriver.ClankMacroDirective directive = cur.getReferencedMacro(); |
93 |
if (directive != null) { |
184 |
if (directive != null) { |
Lines 138-141
Link Here
|
138 |
MacroDeclarationReference macroDeclarationReference = new MacroDeclarationReference(curFile, impl, macroNameOffset); |
229 |
MacroDeclarationReference macroDeclarationReference = new MacroDeclarationReference(curFile, impl, macroNameOffset); |
139 |
res.add(macroDeclarationReference); |
230 |
res.add(macroDeclarationReference); |
140 |
} |
231 |
} |
|
|
232 |
|
233 |
private static void addError(FileImpl curFile, FileContent parsingFileContent, ClankDriver.ClankErrorDirective ppDirective) { |
234 |
CharSequence msg = ppDirective.getMessage(); |
235 |
PreprocHandler.State state = ppDirective.getStateWhenMetErrorDirective(); |
236 |
int start = ppDirective.getDirectiveStartOffset(); |
237 |
int end = ppDirective.getDirectiveEndOffset(); |
238 |
ErrorDirectiveImpl impl = ErrorDirectiveImpl.create(curFile, msg, new CsmOffsetableImpl(curFile, start, end), state); |
239 |
parsingFileContent.addError(impl); |
240 |
} |
241 |
|
242 |
private static void addInclude(FileImpl curFile, FileContent parsingFileContent, ClankDriver.ClankInclusionDirective ppDirective) { |
243 |
ResolvedPath resolvedPath = ppDirective.getResolvedPath(); |
244 |
CharSequence fileName = ppDirective.getSpellingName(); |
245 |
boolean system = ppDirective.isAngled(); |
246 |
boolean broken = (resolvedPath == null); |
247 |
FileImpl includedFile = (FileImpl) ppDirective.getAnnotation(); |
248 |
if ((includedFile == null) != broken) { |
249 |
if (CsmModelAccessor.isModelAlive()) { |
250 |
assert false : "broken " + broken + " vs. " + includedFile; |
251 |
} |
252 |
} |
253 |
int startOffset = ppDirective.getDirectiveStartOffset(); |
254 |
int endOffset = ppDirective.getDirectiveEndOffset(); |
255 |
//boolean hasRecursiveInclude = curFile.equals(includedFile); |
256 |
IncludeImpl incl = IncludeImpl.create(fileName.toString(), system, ppDirective.isRecursive(), includedFile, curFile, startOffset, endOffset); |
257 |
parsingFileContent.addInclude(incl, broken || ppDirective.isRecursive()); |
258 |
} |
259 |
|
260 |
private static final class CsmOffsetableImpl implements CsmOffsetable { |
261 |
|
262 |
private final CsmFile file; |
263 |
private final int selectionStart; |
264 |
private final int selectionEnd; |
265 |
|
266 |
public CsmOffsetableImpl(CsmFile file, int selectionStart, int selectionEnd) { |
267 |
this.file = file; |
268 |
this.selectionStart = selectionStart; |
269 |
this.selectionEnd = selectionEnd; |
270 |
} |
271 |
|
272 |
@Override |
273 |
public CsmFile getContainingFile() { |
274 |
return file; |
275 |
} |
276 |
|
277 |
@Override |
278 |
public int getStartOffset() { |
279 |
return selectionStart; |
280 |
} |
281 |
|
282 |
@Override |
283 |
public int getEndOffset() { |
284 |
return selectionEnd; |
285 |
} |
286 |
|
287 |
@Override |
288 |
public CsmOffsetable.Position getStartPosition() { |
289 |
throw new UnsupportedOperationException(); |
290 |
} |
291 |
|
292 |
@Override |
293 |
public CsmOffsetable.Position getEndPosition() { |
294 |
throw new UnsupportedOperationException(); |
295 |
} |
296 |
|
297 |
@Override |
298 |
public CharSequence getText() { |
299 |
throw new UnsupportedOperationException(); |
300 |
} |
301 |
} |
302 |
|
141 |
} |
303 |
} |