001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.debug;
043:
044: import java.io.FileNotFoundException;
045: import java.io.FileOutputStream;
046: import java.io.PrintStream;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.Comparator;
050: import java.util.HashMap;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Map;
054: import org.netbeans.modules.cnd.api.model.CsmFile;
055:
056: /**
057: *
058: * @author vk155633
059: */
060: public class DiagnosticUnresolved {
061:
062: private static class IntArray {
063:
064: private int[] data;
065: private int size;
066:
067: public IntArray(int capacity) {
068: data = new int[capacity];
069: size = 0;
070: }
071:
072: public IntArray() {
073: this (64);
074: }
075:
076: public int get(int index) {
077: return data[index];
078: }
079:
080: public int size() {
081: return size;
082: }
083:
084: public void add(int value) {
085: if (!contains(value)) {
086: if (size >= data.length) {
087: int[] old = data;
088: data = new int[old.length + 128];
089: }
090: data[size++] = value;
091: }
092: }
093:
094: protected boolean contains(int value) {
095: for (int i = 0; i < size; i++) {
096: if (data[i] == value) {
097: return true;
098: }
099: }
100: return false;
101: }
102:
103: }
104:
105: private static class UnresolvedInfoBase {
106:
107: private String name;
108: private int count;
109:
110: public UnresolvedInfoBase(String name) {
111: this .name = name;
112: }
113:
114: public void registerOccurence(CsmFile file, int offset) {
115: count++;
116: }
117:
118: public int getCount() {
119: return count;
120: }
121:
122: public String getName() {
123: return name;
124: }
125:
126: public void dumpStatistics(PrintStream out) {
127: out.println(getName() + ' ' + getCount());
128: }
129: }
130:
131: private static class UnresolvedInfoEx extends UnresolvedInfoBase {
132:
133: private Map/*<CsmFile, IntArray>*/files = new HashMap()/*<CsmFile, IntArray>*/;
134:
135: public UnresolvedInfoEx(String name) {
136: super (name);
137: }
138:
139: public void registerOccurence(CsmFile file, int offset) {
140: super .registerOccurence(file, offset);
141: IntArray ia = (IntArray) files.get(file);
142: if (ia == null) {
143: ia = new IntArray();
144: files.put(file, ia);
145: }
146: ia.add(offset);
147: }
148:
149: public void dumpStatistics(PrintStream out) {
150:
151: out.println(getName() + ' ' + getCount());
152: out.println(" By files:"); // NOI18N
153:
154: Comparator comp = new Comparator() {
155: public int compare(Object o1, Object o2) {
156: if (o1 == o2) {
157: return 0;
158: }
159: IntArray ia1 = (IntArray) files.get(o1);
160: IntArray ia2 = (IntArray) files.get(o2);
161: return (ia1.size() > ia2.size()) ? -1 : 1;
162: }
163:
164: public boolean equals(Object obj) {
165: return obj == this ;
166: }
167:
168: public int hashCode() {
169: return 5; // any dummy value
170: }
171: };
172:
173: List list = new ArrayList(files.keySet());
174: Collections.sort(list, comp);
175: for (Iterator it = list.iterator(); it.hasNext();) {
176: CsmFile file = (CsmFile) it.next();
177: IntArray ia = (IntArray) files.get(file);
178: int cnt = (ia == null) ? -1 : ia.size();
179: out
180: .println(" " + file.getAbsolutePath() + ' '
181: + cnt); // NOI18N
182: }
183:
184: }
185:
186: }
187:
188: private Map/*<String, UnresolvedInfoBase>*/map = new HashMap();
189: private static int level;
190:
191: public DiagnosticUnresolved(int level) {
192: this .level = level;
193: }
194:
195: private static String glueName(CharSequence[] nameTokens) {
196: StringBuilder sb = new StringBuilder();
197: for (int i = 0; i < nameTokens.length; i++) {
198: if (i > 0) {
199: sb.append("::"); // NOI18N
200: }
201: sb.append(nameTokens[i]);
202: }
203: return sb.toString();
204: }
205:
206: public void onUnresolved(CharSequence[] nameTokens, CsmFile file,
207: int offset) {
208: if (level < 1) {
209: return;
210: }
211: String name = glueName(nameTokens);
212: UnresolvedInfoBase u = (UnresolvedInfoBase) map.get(name);
213: if (u == null) {
214: u = (level == 1) ? new UnresolvedInfoBase(name)
215: : new UnresolvedInfoEx(name);
216: map.put(name, u);
217: }
218: u.registerOccurence(file, offset);
219: }
220:
221: public void dumpStatictics(String fileName, boolean append)
222: throws FileNotFoundException {
223: PrintStream out = new PrintStream(new FileOutputStream(
224: fileName, append), true);
225: try {
226: dumpStatictics(out);
227: } finally {
228: out.close();
229: }
230: }
231:
232: protected void dumpStatictics(PrintStream out) {
233:
234: out.println("\n**** Unresolved names statistics\n"); // NOI18N
235:
236: Comparator comp = new Comparator() {
237: public int compare(Object o1, Object o2) {
238: if (o1 == o2) {
239: return 0;
240: }
241: UnresolvedInfoBase ui1 = (UnresolvedInfoBase) o1;
242: UnresolvedInfoBase ui2 = (UnresolvedInfoBase) o2;
243: return (ui1.getCount() > ui2.getCount()) ? -1 : 1;
244: }
245:
246: public boolean equals(Object obj) {
247: return obj == this ;
248: }
249:
250: public int hashCode() {
251: return 3; // any dummy value
252: }
253: };
254:
255: List infos = new ArrayList(map.values());
256: int total = 0;
257: Collections.sort(infos, comp);
258: for (Iterator it = infos.iterator(); it.hasNext();) {
259: UnresolvedInfoBase ui = (UnresolvedInfoBase) it.next();
260: ui.dumpStatistics(out);
261: total += ui.getCount();
262: }
263:
264: out.println("Totally " + total + " unresolved"); // NOI18N
265: }
266:
267: }
|