Skip to content

Commit 91314d3

Browse files
fourlscirras
authored andcommitted
Rewrite MSBuild parsing for improved language support
This rewrite adds support for - Items - Item metadata expressions - Well-known properties and metadata - Improved property and item handling - Improved conditional expression evaluation
1 parent cbf16be commit 91314d3

35 files changed

+1776
-568
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Support for MSBuild item and item metadata expressions in project files.
1213
- `ExhaustiveEnumCase` analysis rule, which flags `case` statements that do not handle all values in an enumeration.
1314
- **API:** `EnumeratorOccurrence` type.
1415
- **API:** `ForInStatementNode::getEnumeratorOccurrence` method.
@@ -33,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3334
- Detect tab-indented multiline strings in `TabulationCharacter`.
3435
- Improve support for evaluating name references in compiler directive expressions.
3536
- Improve overload resolution in cases involving generic type parameter constraints.
37+
- Improve handling for MSBuild properties, items, and conditional evaluation.
3638

3739
### Deprecated
3840

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Sonar Delphi Plugin
3+
* Copyright (C) 2025 Integrated Application Development
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
18+
*/
19+
package au.com.integradev.delphi.enviroment;
20+
21+
import au.com.integradev.delphi.msbuild.MSBuildParser;
22+
import au.com.integradev.delphi.msbuild.MSBuildState;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.Collections;
26+
import java.util.Map;
27+
28+
public class EnvironmentProjVariableProvider implements EnvironmentVariableProvider {
29+
private final MSBuildState state;
30+
31+
public EnvironmentProjVariableProvider(
32+
Path environmentProj, EnvironmentVariableProvider baseProvider) {
33+
if (environmentProj != null && Files.exists(environmentProj)) {
34+
var parser = new MSBuildParser(environmentProj, baseProvider);
35+
state = parser.parse();
36+
} else {
37+
state =
38+
new MSBuildState(
39+
environmentProj, environmentProj, baseProvider.getenv(), Collections.emptyMap());
40+
}
41+
}
42+
43+
@Override
44+
public Map<String, String> getenv() {
45+
return state.getProperties();
46+
}
47+
48+
@Override
49+
public String getenv(String name) {
50+
return state.getProperty(name);
51+
}
52+
}

delphi-frontend/src/main/java/au/com/integradev/delphi/msbuild/DelphiMSBuildParser.java

Lines changed: 0 additions & 225 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Sonar Delphi Plugin
3+
* Copyright (C) 2025 Integrated Application Development
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
18+
*/
19+
package au.com.integradev.delphi.msbuild;
20+
21+
import au.com.integradev.delphi.enviroment.EnvironmentVariableProvider;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
final class DelphiMSBuildUtils {
30+
private static final Logger LOG = LoggerFactory.getLogger(DelphiMSBuildUtils.class);
31+
32+
private DelphiMSBuildUtils() {
33+
// Utility class
34+
}
35+
36+
public static List<Path> getSourceFiles(MSBuildState state) {
37+
return state.getItems("DCCReference").stream()
38+
.filter(item -> item.getMetadata("Extension").equalsIgnoreCase(".pas"))
39+
.map(MSBuildItem::getPath)
40+
.filter(path -> regularFileExists(path, "DCCReference"))
41+
.collect(Collectors.toList());
42+
}
43+
44+
public static List<DelphiProject> getProjects(
45+
MSBuildState state, EnvironmentVariableProvider environmentVariableProvider) {
46+
return state.getItems("Projects").stream()
47+
.map(MSBuildItem::getPath)
48+
.filter(path -> regularFileExists(path, "Projects"))
49+
.map(
50+
path ->
51+
new DelphiProjectFactory()
52+
.createProject(new MSBuildParser(path, environmentVariableProvider).parse()))
53+
.collect(Collectors.toList());
54+
}
55+
56+
private static boolean regularFileExists(Path path, String specifiedBy) {
57+
if (Files.exists(path) && Files.isRegularFile(path)) {
58+
return true;
59+
} else {
60+
LOG.warn("File specified by {} does not exist: {}", specifiedBy, path);
61+
return false;
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)