Skip to content

Commit

Permalink
Merge branch 'topic/fix_gnatcheck_testsuite' into 'master'
Browse files Browse the repository at this point in the history
Various fixes needed for gnatcheck-testsuite

See merge request eng/libadalang/langkit-query-language!61
  • Loading branch information
Roldak committed Jun 27, 2023
2 parents 326935c + c342118 commit 3e83b0f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 27 deletions.
21 changes: 12 additions & 9 deletions lkql_checker/lalcheck/gnatcheck-source_table.adb
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,8 @@ package body Gnatcheck.Source_Table is

Msg : constant String := To_String (To_Text (Message));

Actual_SF : SF_Id;

begin
-- Only store internal error messages in Debug_Mode for now.
-- Also never store "memoized error" messages which are
Expand All @@ -1522,6 +1524,15 @@ package body Gnatcheck.Source_Table is
Cached_Rule := Rule;
end if;

-- If Follow_Instantiations is True then the relevant file id
-- may not be Next_SF, so perform a lookup.
Actual_SF := File_Find
(Simple_Name (Unit.Filename), Use_Short_Name => True);

if not Present (Actual_SF) then
return;
end if;

if Subprocess_Mode then
Put_Line
(File_Name (Unit) & ":" &
Expand All @@ -1539,15 +1550,7 @@ package body Gnatcheck.Source_Table is
Diagnosis_Kind =>
(if Kind = Rule_Violation then Rule_Violation
else Internal_Error),
SF =>

-- If Follow_Instantiations is True then the relevant file id
-- may not be Next_SF, so perform a lookup.

(if All_Rules.Table (Id).Follow_Instantiations
then File_Find (Simple_Name (Unit.Filename),
Use_Short_Name => True)
else Next_SF),
SF => Actual_SF,
Rule => Id,
Justification => Exemption_Justification (Id));
end if;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public class GNATCheckWorker extends AbstractLanguageLauncher {
*/
private String scenarioVars = null;

/**
* Whether the '--simple-project' flag was used.
*/
private boolean isSimpleProject = false;

/**
* A directory containing all user added rules
*/
Expand Down Expand Up @@ -148,6 +153,9 @@ protected int executeScript(Context.Builder contextBuilder) {
// Set the LKQL language mode to interpreter
contextBuilder.option("lkql.checkerMode", "true");

// If no rules are provided, don't do anything
contextBuilder.option("lkql.fallbackToAllRules", "false");

// Set the context options
if (this.verbose) {
contextBuilder.option("lkql.verbose", "true");
Expand All @@ -162,6 +170,10 @@ protected int executeScript(Context.Builder contextBuilder) {
contextBuilder.option("lkql.scenarioVars", this.scenarioVars);
}

if (this.isSimpleProject) {
contextBuilder.option("lkql.useAutoProvider", "true");
}

// Set the files
if (!this.filesFrom.isEmpty()) {
try {
Expand Down Expand Up @@ -226,14 +238,15 @@ protected int executeScript(Context.Builder contextBuilder) {
* <li> `[-P{project} [--ignore-project-switches]]` The project file to use, and whether to ignore GNATcheck-
* related options that are specified in it.
* <li> `[--simple-project]`
* <li> `[-A]`
* <li> `[-A]` The aggregated project to consider, used when the main project file is an aggregate project.
* <li> `[--RTS {runtime}]` The runtime to use
* <li> `[--target {target}]` The target to use
* <li> `[-d]` Whether to output debug information
* <li> `[-eL]`
* <li> `[--no_objects_dir]` Whether to output data in the objects directory. TODO: Not sure if relevant for workers
* <li> `[--rules-dir {rule_dir}]*` Additional custom directories in which to search for the specified rules
* <li> `[-U]`
* <li> `[-U]` Whether to consider project sources recursively. Not relevant for worker, as files to consider are
* explicitly given by the driver.
* <li> `-files {path}` A path to a temporary file generated by the driver, which contains the list of unit
* names that this worker should be processing during its run.
* <li> `[-Xvar=val]*` The GPR scenario variables to use when loading the project file
Expand Down Expand Up @@ -262,16 +275,28 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
this.projectFile = currentArg.substring(2);
currentArg = iterator.next();

// TODO: handle "--ignore-project-switches"
if (currentArg.equals("--ignore-project-switches")) {
currentArg = iterator.next();
}
}

// TODO: handle "--simple-project"

if (currentArg.equals("--simple-project")) {
currentArg = iterator.next();
this.projectFile = null;
this.isSimpleProject = true;
}

// TODO: handle "-A"
if (currentArg.equals("-A")) {
// Use the specified aggregated project file instead of the aggregate project itself
this.projectFile = iterator.next();

// TODO: handle "-o=..." and "-ox=..."
iterator.next();

currentArg = iterator.next();
}

// TODO: handle "--RTS"

Expand All @@ -298,9 +323,12 @@ protected List<String> preprocessArguments(List<String> arguments, Map<String, S
currentArg = iterator.next();
}

// TODO: handle main unit after "-U"
while (!currentArg.startsWith("-files=")) {
// ignore all files specified after -U: they are already specified by "-files="
currentArg = iterator.next();
}

assert currentArg.equals("-files=");
assert currentArg.startsWith("-files=");
this.filesFrom = currentArg.substring(7);
currentArg = iterator.next();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,13 @@ public void initSources() {
}
}

// If the option is the empty string, the language implementation will end up setting it to the default
// value for its language (e.g. iso-8859-1 for Ada).
String charset = this.env.getOptions().get(LKQLLanguage.charset);

// Get the project file and parse it if there is one
String projectFileName = this.getProjectFile();
Libadalang.UnitProvider provider = null;
final Libadalang.UnitProvider provider;
if (projectFileName != null && !projectFileName.isEmpty() && !projectFileName.isBlank()) {
// Create the project manager
this.projectManager = Libadalang.ProjectManager.create(projectFileName, this.getScenarioVars(), "", "");
Expand All @@ -532,16 +536,22 @@ public void initSources() {
// When no project is specified, `units()` should return the same set of units as `specified_units()`.
this.allSourceFiles = this.specifiedSourceFiles;

// If required, create an auto provider with the specified files.
if (this.env.getOptions().get(LKQLLanguage.useAutoProvider)) {
provider = Libadalang.createAutoProvider(
this.allSourceFiles.toArray(new String[0]),
charset
);
} else {
provider = null;
}

// We should not get any scenario variable if we are being run without a project file.
if (this.getScenarioVars().length != 0) {
System.err.println("Scenario variable specifications require a project file");
}
}

// If the option is the empty string, the language implementation will end up setting it to the default
// value for its language (e.g. iso-8859-1 for Ada).
String charset = this.env.getOptions().get(LKQLLanguage.charset);

// Create the ada context
this.adaContext = Libadalang.AnalysisContext.create(
charset,
Expand Down Expand Up @@ -646,8 +656,8 @@ private void initCheckerCaches() {
}
};

// If there is no wanted rule, just separated the checkers
if (wantedRules == null || wantedRules.length == 0) {
// If there is no wanted rule, run them all (if the appropriate option is set)
if (wantedRules.length == 0 && this.env.getOptions().get(LKQLLanguage.fallbackToAllRules)) {
for (ObjectValue checker : allCheckers.values()) {
dispatchChecker.accept(checker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public enum DiagnosticOutputMode {
)
static final OptionKey<String> scenarioVars = new OptionKey<>("");

/**
* Whether to create an auto provider with the specified files if no project is provided.
*/
@Option(
help = "Whether to create an auto provider with the specified files if no project" +
"is provided.",
category = OptionCategory.USER,
stability = OptionStability.STABLE
)
static final OptionKey<Boolean> useAutoProvider = new OptionKey<>(false);

/**
* The option to define the files to analyze
*/
Expand Down Expand Up @@ -193,12 +204,22 @@ public enum DiagnosticOutputMode {
* The option to specify the rule to run
*/
@Option(
help = "The comma separated rules to apply, if empty apply all the rules",
help = "The comma separated rules to apply",
category = OptionCategory.USER,
stability = OptionStability.STABLE
)
static final OptionKey<String> rules = new OptionKey<>("");

/**
* The option to control what should be done when no rules are provided
*/
@Option(
help = "If true, consider that an empty value for 'rules' means to run all the rules",
category = OptionCategory.USER,
stability = OptionStability.STABLE
)
static final OptionKey<Boolean> fallbackToAllRules = new OptionKey<>(true);

/**
* The option to specify arguments for the rules
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,18 @@ public static void printGNATcheckRuleViolation(
// Append generic instantiation information to the message
if (genericInstantiations.size() > 0) {
StringBuilder messageBuilder = new StringBuilder(message);
messageBuilder.append(" [instance at ");
for (int i = 0; i < genericInstantiations.size(); ++i) {
if (i > 0) {
messageBuilder.append(", ");
messageBuilder.append(" [");
} else {
messageBuilder.append(" [instance at ");
}
final Libadalang.AdaNode inst = genericInstantiations.get(i);
messageBuilder.append(FileUtils.baseName(inst.getUnit().getFileName()));
messageBuilder.append(":");
messageBuilder.append(inst.getSourceLocationRange().start.line);
}
messageBuilder.append("]");
messageBuilder.append("]".repeat(genericInstantiations.size()));
message = messageBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static String[] split(String toSplit, String splitter) {
*/
@CompilerDirectives.TruffleBoundary
public static String[] splitPaths(String toSplit) {
return toSplit.trim().replace(" ", "").split(File.pathSeparator);
return toSplit.trim().split(File.pathSeparator);
}

/**
Expand Down

0 comments on commit 3e83b0f

Please sign in to comment.