-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathMain.java
More file actions
160 lines (149 loc) · 6.33 KB
/
Main.java
File metadata and controls
160 lines (149 loc) · 6.33 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Tai-e: A Static Analysis Framework for Java
*
* Copyright (C) 2022 Tian Tan <tiantan@nju.edu.cn>
* Copyright (C) 2022 Yue Li <yueli@nju.edu.cn>
*
* This file is part of Tai-e.
*
* Tai-e is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Tai-e is distributed in the hope that it will be useful,but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Tai-e. If not, see <https://www.gnu.org/licenses/>.
*/
package pascal.taie;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import pascal.taie.analysis.AnalysisManager;
import pascal.taie.config.AnalysisConfig;
import pascal.taie.config.AnalysisPlanner;
import pascal.taie.config.ConfigManager;
import pascal.taie.config.Configs;
import pascal.taie.config.LoggerConfigs;
import pascal.taie.config.Options;
import pascal.taie.config.Plan;
import pascal.taie.config.PlanConfig;
import pascal.taie.config.Scope;
import pascal.taie.frontend.cache.CachedWorldBuilder;
import pascal.taie.util.IssuePackager;
import pascal.taie.util.RuntimeInfoLogger;
import pascal.taie.util.Timer;
import pascal.taie.util.collection.Lists;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public class Main {
private static final Logger logger = LogManager.getLogger(Main.class);
public static void main(String... args) {
Timer.runAndCount(() -> {
Options options = processArgs(args);
LoggerConfigs.setOutput(options.getOutputDir());
RuntimeInfoLogger.logRuntimeInfo();
Plan plan = processConfigs(options);
if (plan.analyses().isEmpty()) {
logger.info("No analyses are specified");
System.exit(0);
}
buildWorld(options, plan.analyses());
executePlan(plan);
if (options.isCreateIssuePackage()) {
IssuePackager.createIssuePackage(options);
}
}, "Tai-e");
LoggerConfigs.reconfigure();
}
/**
* If the given options is empty or specify to print help information,
* then print help and exit immediately.
*/
private static Options processArgs(String... args) {
Options options = Options.parse(args);
if (options.isPrintHelp() || args.length == 0) {
options.printHelp();
System.exit(0);
}
return options;
}
private static Plan processConfigs(Options options) {
InputStream content = Configs.getAnalysisConfig();
List<AnalysisConfig> analysisConfigs = AnalysisConfig.parseConfigs(content);
ConfigManager manager = new ConfigManager(analysisConfigs);
AnalysisPlanner planner = new AnalysisPlanner(
manager, options.getKeepResult());
boolean reachableScope = options.getScope().equals(Scope.REACHABLE);
if (!options.getAnalyses().isEmpty()) {
// Analyses are specified by options
List<PlanConfig> planConfigs = PlanConfig.readConfigs(options);
manager.overwriteOptions(planConfigs);
Plan plan = planner.expandPlan(
planConfigs, reachableScope);
// Output analysis plan to file.
// For outputting purpose, we first convert AnalysisConfigs
// in the expanded plan to PlanConfigs
planConfigs = Lists.map(plan.analyses(),
ac -> new PlanConfig(ac.getId(), ac.getOptions()));
// TODO: turn off output in testing?
PlanConfig.writeConfigs(planConfigs, options.getOutputDir());
if (!options.isOnlyGenPlan()) {
// This run not only generates plan file but also executes it
return plan;
}
} else if (options.getPlanFile() != null) {
// Analyses are specified by file
List<PlanConfig> planConfigs = PlanConfig.readConfigs(options.getPlanFile());
manager.overwriteOptions(planConfigs);
return planner.makePlan(planConfigs, reachableScope);
}
// No analyses are specified
return Plan.emptyPlan();
}
/**
* Convenient method for building the world from String arguments.
*/
public static void buildWorld(String... args) {
Options options = Options.parse(args);
LoggerConfigs.setOutput(options.getOutputDir());
Plan plan = processConfigs(options);
buildWorld(options, plan.analyses());
LoggerConfigs.reconfigure();
}
private static void buildWorld(Options options, List<AnalysisConfig> analyses) {
Timer.runAndCount(() -> {
try {
Class<? extends WorldBuilder> builderClass = options.getWorldBuilderClass();
Constructor<? extends WorldBuilder> builderCtor = builderClass.getConstructor();
WorldBuilder builder = builderCtor.newInstance();
if (options.isWorldCacheMode()) {
builder = new CachedWorldBuilder(builder);
}
builder.build(options, analyses);
logger.info("{} classes with {} methods in the world",
World.get()
.getClassHierarchy()
.allClasses()
.count(),
World.get()
.getClassHierarchy()
.allClasses()
.mapToInt(c -> c.getDeclaredMethods().size())
.sum());
} catch (InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
System.err.println("Failed to build world due to " + e);
System.exit(1);
}
}, "WorldBuilder");
}
private static void executePlan(Plan plan) {
new AnalysisManager(plan).execute();
}
}