Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import de.peeeq.wurstscript.ast.*;
import de.peeeq.wurstscript.attributes.CofigOverridePackages;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.attributes.names.FuncLink;
import de.peeeq.wurstscript.attributes.names.NameLink;
import de.peeeq.wurstscript.gui.WurstGui;
import de.peeeq.wurstscript.gui.WurstGuiLogger;
import de.peeeq.wurstscript.utils.Utils;
Expand Down Expand Up @@ -936,6 +938,75 @@ private static boolean declaresJassNames(CompilationUnit cu) {
return cu.getCuInfo().getFile().endsWith(".j") || !cu.getJassDecls().isEmpty();
}

/**
* The units of the model, other than the removed ones, which resolve a name to a declaration in a removed unit,
* or which declare a Jass name a removed unit declares too: removing one of two equal Jass names changes how the
* other resolves. The names were resolved by the last check, so this reads what it found.
*/
private static Set<CompilationUnit> usersOfJassNames(List<CompilationUnit> removed, WurstModel model) {
Set<CompilationUnit> removedUnits = Collections.newSetFromMap(new IdentityHashMap<>());
removedUnits.addAll(removed);
Set<String> removedJassNames = new HashSet<>();
for (CompilationUnit cu : removed) {
addJassNames(cu, removedJassNames);
}
Set<CompilationUnit> result = new LinkedHashSet<>();
for (CompilationUnit cu : model) {
if (removedUnits.contains(cu)) {
continue;
}
Set<String> names = new HashSet<>();
addJassNames(cu, names);
names.retainAll(removedJassNames);
if (!names.isEmpty() || resolvesInto(cu, removedUnits)) {
result.add(cu);
}
}
return result;
}

private static void addJassNames(CompilationUnit cu, Set<String> names) {
for (JassToplevelDeclaration declaration : cu.getJassDecls()) {
if (declaration instanceof JassGlobalBlock block) {
for (GlobalVarDef global : block) {
names.add(global.getName());
}
} else if (declaration instanceof NameDef nameDef) {
names.add(nameDef.getName());
}
}
}

private static boolean resolvesInto(CompilationUnit cu, Set<CompilationUnit> units) {
Deque<Element> todo = new ArrayDeque<>();
todo.push(cu);
while (!todo.isEmpty()) {
Element e = todo.pop();
if (e instanceof FuncRef ref) {
FuncLink link = ref.attrFuncLink();
if (link != null && units.contains(link.getDef().attrCompilationUnit())) {
return true;
}
}
if (e instanceof NameRef ref) {
NameLink link = ref.attrNameLink();
if (link != null && units.contains(link.getDef().attrCompilationUnit())) {
return true;
}
}
if (e instanceof TypeRef ref) {
TypeDef def = ref.attrTypeDef();
if (def != null && units.contains(def.attrCompilationUnit())) {
return true;
}
}
for (int i = 0; i < e.size(); i++) {
todo.push(e.get(i));
}
}
return false;
}

@Override
public CompilationUnit replaceCompilationUnitContent(WFile filename, String contents, boolean reportErrors) {
return replaceCompilationUnit(filename, contents, reportErrors);
Expand Down Expand Up @@ -1070,12 +1141,38 @@ public WurstModel getModel() {

@Override
public void retainCompilationUnits(WurstModel model, Predicate<CompilationUnit> keep) {
List<CompilationUnit> toRemove = new ArrayList<>();
for (CompilationUnit cu : model) {
if (!keep.test(cu)) {
toRemove.add(cu);
}
}
if (toRemove.isEmpty()) {
return;
}
Set<CompilationUnit> dependents = Collections.emptySet();
if (model == this.model) {
// What is left has to be checked again only where it may have used what goes: what imports a removed
// package (or one it configures), and, as Jass names are visible everywhere, what resolves a name to a
// removed unit or declares a Jass name a removed unit declares too. A map build removes the units
// nothing imports, a stale Jass file at most, so usually nothing is left to check again.
GlobalCaches.clearLookupCacheFor(toRemove);
Set<String> removedPackages = toRemove.stream()
.flatMap(cu -> cu.getPackages().stream())
.map(WPackage::getName)
.collect(Collectors.toSet());
dependents = calculateCUsToUpdate(Collections.emptyList(), removedPackages, false, model);
if (toRemove.stream().anyMatch(ModelManagerImpl::declaresJassNames)) {
dependents.addAll(usersOfJassNames(toRemove, model));
}
toRemove.forEach(dependents::remove);
clearCompilationUnits(dependents);
}
synchronized (modelLock) {
boolean removed = model.removeIf(cu -> !keep.test(cu));
if (removed && model == this.model) {
model.removeIf(cu -> !keep.test(cu));
if (model == this.model) {
uncheckedUnits.removeIf(cu -> !keep.test(cu));
// what is left may have used what went: it all has to be checked again
uncheckedUnits.addAll(model);
uncheckedUnits.addAll(dependents);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,57 @@ public void theManagerKnowsWhetherItsModelIsChecked() throws IOException {
assertFalse(manager.isFullyChecked(model));
}

/**
* A map build removes the units nothing imports before it compiles. What is left is checked again only where
* it may have used what went: it used to be checked again entirely, which cost the compilation a second check.
*/
@Test
public void purgingWhatNothingUsesKeepsTheModelChecked() throws IOException {
File projectFolder = new File("./temp/testProject_purgeKeepsChecked/");
File wurstFolder = new File(projectFolder, "wurst");
newCleanFolder(wurstFolder);

WFile fileWurst = WFile.create(new File(wurstFolder, "Wurst.wurst"));
WFile fileA = WFile.create(new File(wurstFolder, "A.wurst"));
WFile fileB = WFile.create(new File(wurstFolder, "B.wurst"));
WFile fileUnused = WFile.create(new File(wurstFolder, "Unused.wurst"));
WFile fileC = WFile.create(new File(wurstFolder, "C.wurst"));
WFile fileCConfig = WFile.create(new File(wurstFolder, "C_config.wurst"));
WFile fileJass = WFile.create(new File(wurstFolder, "Extra.j"));
WFile fileStaleJass = WFile.create(new File(wurstFolder, "Stale.j"));
writeFile(fileWurst, "package Wurst\n");
writeFile(fileA, string("package A", "import B", "import C", "public function a() returns int",
" return b() + c() + extra()", ""));
writeFile(fileB, string("package B", "public function b() returns int", " return 1", ""));
writeFile(fileUnused, string("package Unused", "public function unused() returns int", " return 2", ""));
writeFile(fileC, string("package C", "@configurable public constant X = 1", "public function c() returns int", " return X", ""));
writeFile(fileCConfig, string("package C_config", "@config public constant X = 2", ""));
writeFile(fileJass, string("function extra takes nothing returns integer", " return 3", "endfunction", ""));
writeFile(fileStaleJass, string("function stale takes nothing returns integer", " return 4", "endfunction", ""));

ModelManagerImpl manager = new ModelManagerImpl(projectFolder, new BufferManager());
manager.buildProject();
WurstModel model = manager.getModel();
assertTrue(manager.isFullyChecked(model));

manager.retainCompilationUnits(model, cu -> !cu.getCuInfo().getFile().endsWith("Unused.wurst"));
assertTrue(manager.isFullyChecked(model), "nothing used what went");

// a config package is not imported by what it configures, but changes what that resolves to
manager.retainCompilationUnits(model, cu -> !cu.getCuInfo().getFile().endsWith("C_config.wurst"));
assertFalse(manager.isFullyChecked(model), "C and what imports it resolve X differently now");
manager.checkProject(new WurstGuiLogger());
assertTrue(manager.isFullyChecked(model));

// Jass names are visible everywhere, so what matters is whether something resolved one into what goes
manager.retainCompilationUnits(model, cu -> !cu.getCuInfo().getFile().endsWith("Stale.j"));
assertTrue(manager.isFullyChecked(model), "nothing called the stale Jass function");
manager.retainCompilationUnits(model, cu -> !cu.getCuInfo().getFile().endsWith("Extra.j"));
assertFalse(manager.isFullyChecked(model), "A called the Jass function which went");
manager.checkProject(new WurstGuiLogger());
assertTrue(manager.hasErrors(), "the call cannot be resolved any more: " + manager.getFirstErrorDescription());
}

@Test
public void aCompilationDoesNotCheckAModelThatIsAlreadyChecked() throws IOException {
File projectFolder = new File("./temp/testProject_compileSkipsCheck/");
Expand Down
Loading