@@ -123,7 +123,7 @@ public final class LKQLContext {
123123
124124 /** The project's scenario variables. */
125125 @ CompilerDirectives .CompilationFinal (dimensions = 1 )
126- private Libadalang . ScenarioVariable [] scenarioVars = null ;
126+ private String [] scenarioVars = null ;
127127
128128 /** Directories to look for LKQL rules into. */
129129 @ CompilerDirectives .CompilationFinal (dimensions = 1 )
@@ -296,15 +296,13 @@ public TruffleLogger getLogger() {
296296 }
297297
298298 /** Return the list of scenario variables to specify when loading the GPR project file. */
299- public Libadalang . ScenarioVariable [] getScenarioVars () {
299+ public String [] getScenarioVars () {
300300 if (this .scenarioVars == null ) {
301- final var scenarioVarList = new ArrayList <Libadalang . ScenarioVariable >();
301+ final var scenarioVarList = new ArrayList <String >();
302302 for (var entry : this .getOptions ().scenarioVariables ().entrySet ()) {
303- scenarioVarList .add (
304- Libadalang .ScenarioVariable .create (entry .getKey (), entry .getValue ())
305- );
303+ scenarioVarList .add (entry .getKey () + "=" + entry .getValue ());
306304 }
307- this .scenarioVars = scenarioVarList .toArray (new Libadalang . ScenarioVariable [0 ]);
305+ this .scenarioVars = scenarioVarList .toArray (new String [0 ]);
308306 }
309307 return this .scenarioVars ;
310308 }
@@ -424,104 +422,118 @@ public void initSources() {
424422
425423 // Get the project file and use it if there is one
426424 final String projectFileName = this .getProjectFile ();
427- if (!projectFileName .isBlank ()) {
428- this .projectManager = Libadalang .ProjectManager .create (
429- projectFileName ,
430- this .getScenarioVars (),
431- this .getTarget (),
432- this .getRuntime (),
433- this .getConfigFile (),
434- true
435- );
436-
437- // Forward the project diagnostics if there are some
438- if (!this .projectManager .getDiagnostics ().isEmpty ()) {
439- this .getDiagnosticEmitter ()
440- .emitProjectErrors (
441- new File (projectFileName ).getName (),
442- this .projectManager .getDiagnostics ()
443- );
425+ final String [] vars = this .getScenarioVars ();
426+ final String target = this .getTarget ();
427+ final String runtime = this .getRuntime ();
428+ final String configFile = this .getConfigFile ();
429+ try (Libadalang .ProjectOptions opts = new Libadalang .ProjectOptions ();) {
430+ for (var v : vars ) {
431+ opts .addSwitch (Libadalang .ProjectOption .X , v );
432+ }
433+ if (!target .isBlank ()) {
434+ opts .addSwitch (Libadalang .ProjectOption .TARGET , target );
444435 }
436+ if (!runtime .isBlank ()) {
437+ opts .addSwitch (Libadalang .ProjectOption .RTS , runtime );
438+ }
439+ if (!configFile .isBlank ()) {
440+ opts .addSwitch (Libadalang .ProjectOption .CONFIG , configFile );
441+ }
442+ if (!projectFileName .isBlank ()) {
443+ opts .addSwitch (Libadalang .ProjectOption .P , projectFileName );
444+
445+ this .projectManager = new Libadalang .ProjectManager (opts , true );
445446
446- // Get the subproject provided by the user
447- final String [] subprojects =
448- this .getOptions ().subprojectFile ().map (s -> new String [] { s }).orElse (null );
447+ // Forward the project diagnostics if there are some
448+ if (!this .projectManager .getDiagnostics ().isEmpty ()) {
449+ this .getDiagnosticEmitter ()
450+ .emitProjectErrors (
451+ new File (projectFileName ).getName (),
452+ this .projectManager .getDiagnostics ()
453+ );
454+ }
455+
456+ // Get the subproject provided by the user
457+ final String [] subprojects =
458+ this .getOptions ().subprojectFile ().map (s -> new String [] { s }).orElse (null );
459+
460+ // If no files were specified by the user, the files to analyze
461+ // are those of the root project (i.e. without recursing into
462+ // project dependencies)
463+ if (this .specifiedSourceFiles .isEmpty ()) {
464+ this .specifiedSourceFiles .addAll (
465+ Arrays .stream (
466+ this .projectManager .getFiles (
467+ Libadalang .SourceFileMode .ROOT_PROJECT ,
468+ subprojects
469+ )
470+ ).toList ()
471+ );
472+ }
449473
450- // If no files were specified by the user, the files to analyze are those of the root
451- // project (i.e. without recursing into project dependencies)
452- if ( this . specifiedSourceFiles . isEmpty ()) {
453- this .specifiedSourceFiles .addAll (
474+ // The `units()` built-in function must return all units of the
475+ // project including units from its dependencies. So let's
476+ // retrieve all those files as well.
477+ this .allSourceFiles .addAll (
454478 Arrays .stream (
455479 this .projectManager .getFiles (
456- Libadalang .SourceFileMode .ROOT_PROJECT ,
480+ Libadalang .SourceFileMode .WHOLE_PROJECT ,
457481 subprojects
458482 )
459483 ).toList ()
460484 );
485+
486+ this .analysisContext = this .projectManager .createContext (
487+ this .getOptions ().subprojectFile ().orElse (null ),
488+ this .eventHandler ,
489+ true ,
490+ 8
491+ );
461492 }
493+ // Else, either load the implicit project.
494+ else {
495+ opts .addSwitch (Libadalang .ProjectOption .NO_PROJECT );
462496
463- // The `units()` built-in function must return all units of the project including units
464- // from its dependencies. So let's retrieve all those files as well.
465- this .allSourceFiles .addAll (
466- Arrays .stream (
467- this .projectManager .getFiles (
468- Libadalang .SourceFileMode .WHOLE_PROJECT ,
469- subprojects
470- )
471- ).toList ()
472- );
497+ // We should not get any scenario variable if we are being run
498+ // without a project file.
499+ if (vars .length != 0 ) {
500+ throw LKQLRuntimeException .fromMessage (
501+ "Scenario variable specifications require a project file"
502+ );
503+ }
504+
505+ // If the option is the empty string, the language
506+ // implementation will end up setting it to the default value
507+ // for its language (e.g. iso-8859-1 for Ada).
508+ String charset = this .getOptions ().charset ().orElse ("" );
473509
474- this .analysisContext = this .projectManager .createContext (
475- this .getOptions ().subprojectFile ().orElse (null ),
510+ // Load the implicit project
511+ this .projectManager = new Libadalang .ProjectManager (opts , true );
512+ this .allSourceFiles .addAll (
513+ Arrays .stream (
514+ this .projectManager .getFiles (Libadalang .SourceFileMode .WHOLE_PROJECT )
515+ ).toList ()
516+ );
517+ final Libadalang .UnitProvider provider = this .projectManager .getProvider ();
518+
519+ // Create the ada context and store it in the LKQL context
520+ /*
521+ * TODO: Genericize LKQL or Java issue #502. Requires to make create static but not possible
522+ * via an interface nor an abstract class.
523+ */
524+ this .analysisContext = Libadalang .AnalysisContext .create (
525+ charset ,
526+ null ,
527+ provider ,
476528 this .eventHandler ,
477529 true ,
478530 8
479531 );
480- }
481- // Else, either load the implicit project.
482- else {
483- // We should not get any scenario variable if we are being run without a project file.
484- if (this .getScenarioVars ().length != 0 ) {
485- throw LKQLRuntimeException .fromMessage (
486- "Scenario variable specifications require a project file"
487- );
488- }
489532
490- // If the option is the empty string, the language implementation will end up setting it
491- // to the default value for its language (e.g. iso-8859-1 for Ada).
492- String charset = this .getOptions ().charset ().orElse ("" );
493-
494- // Load the implicit project
495- this .projectManager = Libadalang .ProjectManager .createImplicit (
496- this .getTarget (),
497- this .getRuntime (),
498- this .getConfigFile (),
499- true
500- );
501- this .allSourceFiles .addAll (
502- Arrays .stream (
503- this .projectManager .getFiles (Libadalang .SourceFileMode .WHOLE_PROJECT )
504- ).toList ()
505- );
506- final Libadalang .UnitProvider provider = this .projectManager .getProvider ();
507-
508- // Create the ada context and store it in the LKQL context
509- /*
510- * TODO: Genericize LKQL or Java issue #502. Requires to make create static but not possible
511- * via an interface nor an abstract class.
512- */
513- this .analysisContext = Libadalang .AnalysisContext .create (
514- charset ,
515- null ,
516- provider ,
517- this .eventHandler ,
518- true ,
519- 8
520- );
521-
522- // In the absence of a project file, we consider for now that there are no configuration
523- // pragmas.
524- this .analysisContext .setConfigPragmasMapping (null , null );
533+ // In the absence of a project file, we consider for now that there are no configuration
534+ // pragmas.
535+ this .analysisContext .setConfigPragmasMapping (null , null );
536+ }
525537 }
526538 }
527539
0 commit comments