This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 253863
Collapse All | Expand All

(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/parser/clank/ClankMacroUsagesSupport.java (-4 / +166 lines)
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
}
(-)a/cnd.modelimpl/src/org/netbeans/modules/cnd/modelimpl/parser/clank/ClankTokenStreamProducer.java (-157 / +4 lines)
Lines 173-181 Link Here
173
          return null;
173
          return null;
174
        }
174
        }
175
        if (super.isFromEnsureParsed()) {
175
        if (super.isFromEnsureParsed()) {
176
          addPreprocessorDirectives(fileImpl, getFileContent(), tokStreamCache);
176
          ClankMacroUsagesSupport.addPreprocessorDirectives(fileImpl, getFileContent(), tokStreamCache);
177
          addMacroExpansions(fileImpl, getFileContent(), getStartFile(), tokStreamCache);
177
          ClankMacroUsagesSupport.addMacroExpansions(fileImpl, getFileContent(), getStartFile(), tokStreamCache);
178
          setFileGuard(fileImpl, getFileContent(), tokStreamCache);
178
          ClankMacroUsagesSupport.setFileGuard(fileImpl, getFileContent(), tokStreamCache);
179
        }
179
        }
180
        skipped = tokStreamCache.getSkippedRanges();
180
        skipped = tokStreamCache.getSkippedRanges();
181
        if (parameters.applyLanguageFilter) {
181
        if (parameters.applyLanguageFilter) {
Lines 609-766 Link Here
609
        }
609
        }
610
    }
610
    }
611
611
612
    private static void addPreprocessorDirectives(FileImpl curFile, FileContent parsingFileContent, ClankDriver.APTTokenStreamCache cache) {
613
        assert parsingFileContent != null;
614
        assert curFile != null;
615
        assert cache != null;
616
        for (ClankDriver.ClankPreprocessorDirective cur : cache.getPreprocessorDirectives()) {
617
            if (cur instanceof ClankDriver.ClankInclusionDirective) {
618
                addInclude(curFile, parsingFileContent, (ClankDriver.ClankInclusionDirective)cur);
619
            } else if (cur instanceof ClankDriver.ClankErrorDirective) {
620
                addError(curFile, parsingFileContent, (ClankDriver.ClankErrorDirective)cur);
621
            } else if (cur instanceof ClankMacroDirective) {
622
                addMacro(curFile, parsingFileContent, (ClankMacroDirective)cur);
623
            } else {
624
              CndUtils.assertTrueInConsole(false, "unknown directive " + cur.getClass().getSimpleName() + " " + cur);
625
            }
626
        }
627
    }
628
    
629
    private static void setFileGuard(FileImpl curFile, FileContent parsingFileContent, ClankDriver.APTTokenStreamCache cache) {
630
        ClankDriver.FileGuard fileGuard = cache.getFileGuard();
631
        if (fileGuard != null) {
632
            curFile.setFileGuard(fileGuard.getStartOfset(), fileGuard.getEndOfset());
633
        } else {
634
            curFile.setFileGuard(-1, -1);
635
        }
636
    }
637
638
    private static void addMacroExpansions(FileImpl curFile, FileContent parsingFileContent, FileImpl startFile, ClankDriver.APTTokenStreamCache cache) {
639
        for (ClankDriver.MacroExpansion cur : cache.getMacroExpansions()) {
640
            ClankMacroDirective directive = cur.getReferencedMacro();
641
            if (directive != null) {
642
                MacroReference macroRef = MacroReference.createMacroReference(curFile, cur.getStartOfset(), cur.getStartOfset()+cur.getMacroNameLength(), startFile, directive);
643
                if (macroRef == null) {
644
                    if (!curFile.isValid()) {
645
                        break;
646
                    }
647
                } else {
648
                    addMacroUsage(curFile, parsingFileContent, macroRef);
649
                }
650
            } else {
651
                // TODO: process invalid macro definition
652
                assert false : "Not found referenced ClankMacroDirective "+cur;
653
            }
654
        }
655
        for(ClankDriver.MacroUsage cur : cache.getMacroUsages()) {
656
            ClankMacroDirective directive = cur.getReferencedMacro();
657
            if (directive != null) {
658
                MacroReference macroRef = MacroReference.createMacroReference(curFile, cur.getStartOfset(), cur.getEndOfset(), startFile, directive);
659
                if (macroRef == null) {
660
                    if (!curFile.isValid()) {
661
                        break;
662
                    }
663
                } else {
664
                    addMacroUsage(curFile, parsingFileContent, macroRef);
665
                }
666
            } else {
667
                // TODO: process invalid macro definition
668
                assert false : "Not found referenced ClankMacroDirective "+cur;
669
            }
670
        }
671
    }
672
673
    private static void addMacroUsage(FileImpl curFile, FileContent parsingFileContent, MacroReference macroReference) {
674
        parsingFileContent.addReference(macroReference, macroReference.getReferencedObject());
675
    }
676
677
    private static void addMacro(FileImpl curFile, FileContent parsingFileContent, ClankMacroDirective ppDirective) {
678
        if (!ppDirective.isDefined()) {
679
            // only #define are handled by old model, not #undef
680
            return;
681
        }
682
        CsmMacro.Kind kind = CsmMacro.Kind.DEFINED;
683
        List<CharSequence> params = ppDirective.getParameters();
684
        CharSequence name = ppDirective.getMacroName();
685
        String body = "";
686
        int startOffset = ppDirective.getDirectiveStartOffset();
687
        int endOffset = ppDirective.getDirectiveEndOffset();
688
        int macroNameOffset = ppDirective.getMacroNameOffset();
689
        CsmMacro impl = MacroImpl.create(name, params, body/*sb.toString()*/, curFile, startOffset, endOffset, kind);
690
        parsingFileContent.addMacro(impl);
691
        parsingFileContent.addReference(new MacroDeclarationReference(curFile, impl, macroNameOffset), impl);
692
    }
693
694
    private static void addError(FileImpl curFile, FileContent parsingFileContent, ClankDriver.ClankErrorDirective ppDirective) {
695
        CharSequence msg = ppDirective.getMessage();
696
        PreprocHandler.State state = ppDirective.getStateWhenMetErrorDirective();
697
        int start = ppDirective.getDirectiveStartOffset();
698
        int end = ppDirective.getDirectiveEndOffset();
699
        ErrorDirectiveImpl impl = ErrorDirectiveImpl.create(curFile, msg, new CsmOffsetableImpl(curFile, start, end), state);
700
        parsingFileContent.addError(impl);
701
    }
702
703
    private static void addInclude(FileImpl curFile, FileContent parsingFileContent, ClankDriver.ClankInclusionDirective ppDirective) {
704
        ResolvedPath resolvedPath = ppDirective.getResolvedPath();
705
        CharSequence fileName = ppDirective.getSpellingName();
706
        boolean system = ppDirective.isAngled();
707
        boolean broken = (resolvedPath == null);
708
        FileImpl includedFile = (FileImpl) ppDirective.getAnnotation();
709
        if ((includedFile == null) != broken) {
710
            if (CsmModelAccessor.isModelAlive()) {
711
                assert false : "broken " + broken + " vs. " + includedFile;
712
            }
713
        }
714
        int startOffset = ppDirective.getDirectiveStartOffset();
715
        int endOffset = ppDirective.getDirectiveEndOffset();
716
        //boolean hasRecursiveInclude = curFile.equals(includedFile);
717
        IncludeImpl incl = IncludeImpl.create(fileName.toString(), system, ppDirective.isRecursive(), includedFile, curFile, startOffset, endOffset);
718
        parsingFileContent.addInclude(incl, broken || ppDirective.isRecursive());
719
    }
720
721
    private static final class CsmOffsetableImpl implements CsmOffsetable {
722
723
        private final CsmFile file;
724
        private final int selectionStart;
725
        private final int selectionEnd;
726
727
        public CsmOffsetableImpl(CsmFile file, int selectionStart, int selectionEnd) {
728
            this.file = file;
729
            this.selectionStart = selectionStart;
730
            this.selectionEnd = selectionEnd;
731
        }
732
733
        @Override
734
        public CsmFile getContainingFile() {
735
            return file;
736
        }
737
738
        @Override
739
        public int getStartOffset() {
740
            return selectionStart;
741
        }
742
743
        @Override
744
        public int getEndOffset() {
745
            return selectionEnd;
746
        }
747
748
        @Override
749
        public Position getStartPosition() {
750
            throw new UnsupportedOperationException();
751
        }
752
753
        @Override
754
        public Position getEndPosition() {
755
            throw new UnsupportedOperationException();
756
        }
757
758
        @Override
759
        public CharSequence getText() {
760
            throw new UnsupportedOperationException();
761
        }
762
    }
763
764
    private static final class PatchedFileBuffer implements FileBuffer {
612
    private static final class PatchedFileBuffer implements FileBuffer {
765
        private final FileBuffer delegate;
613
        private final FileBuffer delegate;
766
        private final CodePatch codePatch;
614
        private final CodePatch codePatch;
Lines 871-875 Link Here
871
        }
719
        }
872
720
873
    }
721
    }
874
722
}
875
            }

Return to bug 253863