Skip to content

Commit

Permalink
Also for includes
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser committed Oct 17, 2024
1 parent 511d4d2 commit a332b07
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ class CdtParser(config: Config, compilationDatabase: List[CommandObject])
val additionalIncludes =
if (FileDefaults.isCPPFile(file.toString)) parserConfig.systemIncludePathsCPP
else parserConfig.systemIncludePathsC
val fileSpecificDefines = parserConfig.definedSymbolsPerFile.getOrElse(file.toString, Map.empty)
val fileSpecificDefines = parserConfig.definedSymbolsPerFile.getOrElse(file.toString, Map.empty)
val fileSpecificIncludes = parserConfig.includesPerFile.getOrElse(file.toString, List.empty)
new ScannerInfo(
(definedSymbols ++ fileSpecificDefines).asJava,
(includePaths ++ additionalIncludes).map(_.toString).toArray
fileSpecificIncludes.toArray ++ (includePaths ++ additionalIncludes).map(_.toString).toArray
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ object JSONCompilationDatabaseParser {
*/
private val defineInCommandPattern = """-D([A-Za-z_][A-Za-z0-9_]+)(=(\\*".*"))?""".r

/** {{{
* 1) -I: Matches the -I flag, which indicates an include directory.
* 2) (\S+): Matches one or more non-whitespace characters, which represent the path of the directory.
* }}}
*/
private val includeInCommandPattern = """-I(\S+)""".r

case class CommandObject(directory: String, arguments: List[String], command: List[String], file: String) {

/** @return
Expand All @@ -39,6 +46,17 @@ object JSONCompilationDatabaseParser {
}
}

private def pathFromInclude(include: String): String = include.stripPrefix("-I")

def includes(): List[String] = {
val includesFromArguments = arguments.filter(a => a.startsWith("-I")).map(pathFromInclude)
val includesFromCommand = command.flatMap { c =>
val includes = includeInCommandPattern.findAllIn(c).toList
includes.map(pathFromInclude)
}
includesFromArguments ++ includesFromCommand
}

def defines(): List[(String, String)] = {
val definesFromArguments = arguments.filter(a => a.startsWith("-D")).map(nameValuePairFromDefine)
val definesFromCommand = command.flatMap { c =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ import java.nio.file.{Path, Paths}
object ParserConfig {

def empty: ParserConfig =
ParserConfig(Set.empty, Set.empty, Set.empty, Map.empty, Map.empty, logProblems = false, logPreprocessor = false)
ParserConfig(
Set.empty,
Set.empty,
Set.empty,
Map.empty,
Map.empty,
Map.empty,
logProblems = false,
logPreprocessor = false
)

def fromConfig(config: Config, compilationDatabase: List[CommandObject]): ParserConfig = {
val compilationDatabaseDefines = compilationDatabase.map { c =>
c.compiledFile() -> c.defines().toMap
}.toMap
val includes = compilationDatabase.map { c =>
c.compiledFile() -> c.includes()
}.toMap
ParserConfig(
config.includePaths.map(Paths.get(_).toAbsolutePath),
IncludeAutoDiscovery.discoverIncludePathsC(config),
Expand All @@ -26,6 +38,7 @@ object ParserConfig {
case define => define -> "true"
}.toMap ++ DefaultDefines.DEFAULT_CALL_CONVENTIONS,
compilationDatabaseDefines,
includes,
config.logProblems,
config.logPreprocessor
)
Expand All @@ -39,6 +52,7 @@ case class ParserConfig(
systemIncludePathsCPP: Set[Path],
definedSymbols: Map[String, String],
definedSymbolsPerFile: Map[String, Map[String, String]],
includesPerFile: Map[String, List[String]],
logProblems: Boolean,
logPreprocessor: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class JSONCompilationDatabaseParserTests extends AnyWordSpec with Matchers {
"""
|[
| { "directory": "/home/user/llvm/build",
| "arguments": ["/usr/bin/clang++", "-Irelative", "-DSOMEDEFA=With spaces, quotes and \\-es.", "-c", "-o", "file.o", "file.cc"],
| "arguments": ["/usr/bin/clang++", "-I/usr/include", "-I./include", "-DSOMEDEFA=With spaces, quotes and \\-es.", "-c", "-o", "file.o", "file.cc"],
| "file": "file.cc" },
| { "directory": "/home/user/llvm/build",
| "command": "/usr/bin/clang++ -Irelative -DSOMEDEFB=\"With spaces, quotes and \\-es.\" -DSOMEDEFC -c -o file.o file.cc",
| "command": "/usr/bin/clang++ -I/home/user/project/includes -DSOMEDEFB=\"With spaces, quotes and \\-es.\" -DSOMEDEFC -c -o file.o file.cc",
| "file": "file2.cc" }
|]""".stripMargin

Expand All @@ -39,8 +39,8 @@ class JSONCompilationDatabaseParserTests extends AnyWordSpec with Matchers {
("SOMEDEFB", "\"With spaces, quotes and \\-es.\""),
("SOMEDEFC", "1")
)
commandObjects.flatMap(_.includes()) shouldBe List("/usr/include", "./include", "/home/user/project/includes")
}

}
}

Expand Down

0 comments on commit a332b07

Please sign in to comment.