Skip to main content
summaryrefslogtreecommitdiffstats
blob: d2c3e6348629d4a64d6eb013327a70bfc0d5ea3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.text.tool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.text.FindResults;
import org.eclipse.osee.framework.jdk.core.util.Lib;

/**
 * @author Ryan D. Brooks
 */
public class FunctionCallStats {
   @SuppressWarnings("unchecked")
   public static void main(String[] args) throws IOException {
      if (args.length < 2) {
         System.out.println("Usage: FunctionCallStats <search directory> <fileName pattern>");
         return;
      }

      ArrayList<String> patterns = new ArrayList<String>();
      patterns.add("\\W(\\w+)\\s*\\([^;{]*?\\)\\s*;");

      BufferedWriter out = new BufferedWriter(new FileWriter("results.csv"));

      List files = Lib.recursivelyListFiles(new File(args[0]), Pattern.compile(args[1]));
      System.out.println("Searching " + files.size() + " files...");

      FindNonLocalFunctionCalls nonLocalFindApp =
            new FindNonLocalFunctionCalls((File[]) files.toArray(new File[files.size()]));
      nonLocalFindApp.searchFiles();
      Set nonLocalFunctions = nonLocalFindApp.getResultSet();

      Find app = new Find(patterns, files, new StripBlockComments());
      app.setRegionPadding(0, 0);
      app.find(999999, true);
      FindResults results = app.getResults();

      class Counter {
         public int count = 0;
      }

      String lastFileName = null;
      HashMap functions = new HashMap(1000);
      for (FindResults.FindResultsIterator i = results.iterator(); i.hasNext();) {
         String currentFileName = i.currentFile.getName();

         if (i.currentRegion != null) {
            if (!currentFileName.equals(lastFileName)) {
               for (Iterator functionsIterator = functions.entrySet().iterator(); functionsIterator.hasNext();) {
                  Map.Entry entry = (Map.Entry) functionsIterator.next();
                  String functionName = (String) entry.getKey();
                  if (nonLocalFunctions.contains(functionName)) {
                     out.write(lastFileName);
                     out.write(',');
                     out.write(functionName);
                     out.write(',');
                     out.write(String.valueOf(((Counter) entry.getValue()).count));
                     out.write('\n');
                  }
               }
               lastFileName = currentFileName;
               functions.clear();
            }

            Counter counter = (Counter) functions.get(i.currentRegion);
            if (counter == null) {
               counter = new Counter();
            }
            counter.count++;
            functions.put(i.currentRegion, counter);
         }
      }
      out.close();
   }
}

Back to the top