@@ -41,6 +41,10 @@ public class JsonCompilationDatabase {
4141
4242 private static final Logger LOG = Loggers .get (JsonCompilationDatabase .class );
4343
44+ enum ArgNext {
45+ NONE , DEFINE , INCLUDE , IQUOTE , ISYSTEM , IDIRAFTER ;
46+ }
47+
4448 private JsonCompilationDatabase () {
4549 /* utility class is not meant to be instantiated */
4650 }
@@ -61,99 +65,111 @@ public static void parse(CxxConfiguration config, File compileCommandsFile) thro
6165 mapper .enable (DeserializationFeature .USE_JAVA_ARRAY_FOR_JSON_ARRAY );
6266 mapper .enable (DeserializationFeature .ACCEPT_SINGLE_VALUE_AS_ARRAY );
6367
64- JsonCompilationDatabaseCommandObject [] commandObjects = mapper .readValue (compileCommandsFile ,
65- JsonCompilationDatabaseCommandObject [].class );
68+ JsonCompilationDatabaseCommandObject [] commandObjects
69+ = mapper .readValue (compileCommandsFile , JsonCompilationDatabaseCommandObject [].class );
70+ Path cwd ;
6671
6772 for (JsonCompilationDatabaseCommandObject commandObject : commandObjects ) {
68-
69- Path cwd = Paths .get ("." );
70-
7173 if (commandObject .getDirectory () != null ) {
7274 cwd = Paths .get (commandObject .getDirectory ());
75+ } else {
76+ cwd = Paths .get ("." );
7377 }
7478
7579 Path absPath = cwd .resolve (commandObject .getFile ());
80+ CxxCompilationUnitSettings settings = new CxxCompilationUnitSettings ();
81+ parseCommandObject (settings , cwd , commandObject );
7682
7783 if ("__global__" .equals (commandObject .getFile ())) {
78- CxxCompilationUnitSettings globalSettings = new CxxCompilationUnitSettings ();
79-
80- parseCommandObject (globalSettings , cwd , commandObject );
81-
82- config .setGlobalCompilationUnitSettings (globalSettings );
84+ config .setGlobalCompilationUnitSettings (settings );
8385 } else {
84- CxxCompilationUnitSettings settings = new CxxCompilationUnitSettings ();
85-
86- parseCommandObject (settings , cwd , commandObject );
87-
8886 config .addCompilationUnitSettings (absPath .toAbsolutePath ().normalize ().toString (), settings );
8987 }
9088 }
9189 }
9290
9391 private static void parseCommandObject (CxxCompilationUnitSettings settings ,
94- Path cwd ,
95- JsonCompilationDatabaseCommandObject commandObject ) {
92+ Path cwd , JsonCompilationDatabaseCommandObject commandObject ) {
93+
9694 settings .setDefines (commandObject .getDefines ());
9795 settings .setIncludes (commandObject .getIncludes ());
9896
9997 // No need to parse command lines as we have needed information
100- if (! commandObject .getDefines (). isEmpty () || ! commandObject .getIncludes (). isEmpty ()) {
98+ if (commandObject .hasDefines () || commandObject .hasIncludes ()) {
10199 return ;
102100 }
103101
104102 String cmdLine ;
105103
106- if (! commandObject .getArguments (). isEmpty ()) {
104+ if (commandObject .hasArguments ()) {
107105 cmdLine = commandObject .getArguments ().stream ().collect (Collectors .joining (" " ));
108- } else if (! commandObject .getCommand (). isEmpty ()) {
106+ } else if (commandObject .hasCommand ()) {
109107 cmdLine = commandObject .getCommand ();
110108 } else {
111109 return ;
112110 }
113111
114112 String [] args = tokenizeCommandLine (cmdLine );
115- boolean nextInclude = false ;
116- boolean nextDefine = false ;
117- List <Path > includes = new ArrayList <>();
113+ ArgNext next = ArgNext .NONE ;
114+
118115 HashMap <String , String > defines = new HashMap <>();
116+ List <Path > includes = new ArrayList <>();
117+ List <Path > iSystem = new ArrayList <>();
118+ List <Path > iDirAfter = new ArrayList <>();
119119
120- // Capture defines and includes from command line
121120 for (String arg : args ) {
122- if (nextInclude ) {
123- nextInclude = false ;
124- includes .add (makeRelativeToCwd (cwd , arg ));
125- } else if (nextDefine ) {
126- nextDefine = false ;
127- String [] define = arg .split ("=" , 2 );
128- if (define .length == 1 ) {
129- defines .put (define [0 ], "" );
130- } else {
131- defines .put (define [0 ], define [1 ]);
132- }
133- } else if ("-I" .equals (arg )) {
134- nextInclude = true ;
135- } else if ("-isystem" .equals (arg )) {
136- nextInclude = true ;
121+ if (arg .startsWith ("-D" )) {
122+ arg = arg .substring (2 );
123+ next = ArgNext .DEFINE ;
137124 } else if (arg .startsWith ("-I" )) {
138- includes .add (makeRelativeToCwd (cwd , arg .substring (2 )));
125+ arg = arg .substring (2 );
126+ next = ArgNext .INCLUDE ;
127+ } else if (arg .startsWith ("-iquote" )) {
128+ arg = arg .substring (7 );
129+ next = ArgNext .INCLUDE ;
139130 } else if (arg .startsWith ("-isystem" )) {
140- includes .add (makeRelativeToCwd (cwd , arg .substring (8 )));
141- } else if ("-D" .equals (arg )) {
142- nextDefine = true ;
143- } else if (arg .startsWith ("-D" )) {
144- String [] define = arg .substring (2 ).split ("=" , 2 );
145- if (define .length == 1 ) {
146- defines .put (define [0 ], "" );
147- } else {
148- defines .put (define [0 ], define [1 ]);
131+ arg = arg .substring (8 );
132+ next = ArgNext .ISYSTEM ;
133+ } else if (arg .startsWith ("-idirafter" )) {
134+ arg = arg .substring (10 );
135+ next = ArgNext .IDIRAFTER ;
136+ }
137+
138+ if ((next != ArgNext .NONE ) && !arg .isEmpty ()) {
139+ switch (next ) {
140+ case DEFINE :
141+ addMacro (arg , defines );
142+ break ;
143+ case INCLUDE :
144+ case IQUOTE :
145+ includes .add (makeRelativeToCwd (cwd , arg ));
146+ break ;
147+ case ISYSTEM :
148+ iSystem .add (makeRelativeToCwd (cwd , arg ));
149+ break ;
150+ case IDIRAFTER :
151+ iDirAfter .add (makeRelativeToCwd (cwd , arg ));
152+ break ;
149153 }
154+ next = ArgNext .NONE ;
150155 }
151156 }
152157
153158 settings .setDefines (defines );
159+ includes .addAll (iSystem );
160+ includes .addAll (iDirAfter );
154161 settings .setIncludes (includes );
155162 }
156163
164+ private static void addMacro (String keyValue , HashMap <String , String > defines ) {
165+ String [] strings = keyValue .split ("=" , 2 );
166+ if (strings .length == 1 ) {
167+ defines .put (strings [0 ], "1" );
168+ } else {
169+ defines .put (strings [0 ], strings [1 ]);
170+ }
171+ }
172+
157173 private static Path makeRelativeToCwd (Path cwd , String include ) {
158174 return cwd .resolve (include ).normalize ();
159175 }
0 commit comments