@@ -10,62 +10,13 @@ import io.shiftleft.x2cpg.{SourceFiles, X2Cpg, X2CpgConfig}
10
10
import org .slf4j .LoggerFactory
11
11
import scopt .OParser
12
12
13
- import java .nio .file .Files
14
13
import java .util .concurrent .ConcurrentHashMap
15
- import scala .collection .mutable .ListBuffer
16
14
import scala .jdk .CollectionConverters ._
17
15
import scala .util .control .NonFatal
18
16
19
17
case class Global (usedTypes : ConcurrentHashMap [String , Boolean ] = new ConcurrentHashMap [String , Boolean ]())
20
18
21
19
class FuzzyC2Cpg () {
22
- import FuzzyC2Cpg .logger
23
-
24
- def runWithPreprocessorAndOutput (sourcePaths : Set [String ],
25
- sourceFileExtensions : Set [String ],
26
- includeFiles : Set [String ],
27
- includePaths : Set [String ],
28
- defines : Set [String ],
29
- undefines : Set [String ],
30
- preprocessorExecutable : String ,
31
- optionalOutputPath : Option [String ] = None ): Unit = {
32
- // Create temp dir to store preprocessed source.
33
- val preprocessedPath = Files .createTempDirectory(" fuzzyc2cpg_preprocessed_" )
34
- logger.info(s " Writing preprocessed files to [ $preprocessedPath] " )
35
-
36
- val preprocessorLogFile = Files .createTempFile(" fuzzyc2cpg_preprocessor_log" , " .txt" ).toFile
37
- logger.info(s " Writing preprocessor logs to [ $preprocessorLogFile] " )
38
-
39
- val sourceFileNames = SourceFiles .determine(sourcePaths, sourceFileExtensions)
40
-
41
- val commandBuffer = new ListBuffer [String ]()
42
- commandBuffer.appendAll(List (preprocessorExecutable, " --verbose" , " -o" , preprocessedPath.toString))
43
- if (sourceFileNames.nonEmpty) commandBuffer.appendAll(List (" -f" , sourceFileNames.mkString(" ," )))
44
- if (includeFiles.nonEmpty) commandBuffer.appendAll(List (" --include" , includeFiles.mkString(" ," )))
45
- if (includePaths.nonEmpty) commandBuffer.appendAll(List (" -I" , includePaths.mkString(" ," )))
46
- if (defines.nonEmpty) commandBuffer.appendAll(List (" -D" , defines.mkString(" ," )))
47
- if (undefines.nonEmpty) commandBuffer.appendAll(List (" -U" , defines.mkString(" ," )))
48
-
49
- val cmd = commandBuffer.toList
50
-
51
- // Run preprocessor
52
- logger.info(" Running preprocessor..." )
53
- val process = new ProcessBuilder ()
54
- .redirectOutput(preprocessorLogFile)
55
- .redirectError(preprocessorLogFile)
56
- .command(cmd : _* )
57
- .start()
58
- val exitCode = process.waitFor()
59
-
60
- if (exitCode == 0 ) {
61
- logger.info(s " Preprocessing complete, files written to [ $preprocessedPath], starting CPG generation... " )
62
- val cpg = runAndOutput(Set (preprocessedPath.toString), sourceFileExtensions, optionalOutputPath)
63
- cpg.close()
64
- } else {
65
- logger.error(
66
- s " Error occurred whilst running preprocessor. Log written to [ $preprocessorLogFile]. Exit code [ $exitCode]. " )
67
- }
68
- }
69
20
70
21
def runAndOutput (sourcePaths : Set [String ],
71
22
sourceFileExtensions : Set [String ],
@@ -93,15 +44,8 @@ object FuzzyC2Cpg {
93
44
94
45
final case class Config (inputPaths : Set [String ] = Set .empty,
95
46
outputPath : String = X2CpgConfig .defaultOutputPath,
96
- sourceFileExtensions : Set [String ] = Set (" .c" , " .cc" , " .cpp" , " .h" , " .hpp" ),
97
- includeFiles : Set [String ] = Set .empty,
98
- includePaths : Set [String ] = Set .empty,
99
- defines : Set [String ] = Set .empty,
100
- undefines : Set [String ] = Set .empty,
101
- preprocessorExecutable : String = " ./fuzzypp/bin/fuzzyppcli" )
47
+ sourceFileExtensions : Set [String ] = Set (" .c" , " .cc" , " .cpp" , " .h" , " .hpp" ))
102
48
extends X2CpgConfig [Config ] {
103
- lazy val usePreprocessor : Boolean =
104
- includeFiles.nonEmpty || includePaths.nonEmpty || defines.nonEmpty || undefines.nonEmpty
105
49
106
50
override def withAdditionalInputPath (inputPath : String ): Config = copy(inputPaths = inputPaths + inputPath)
107
51
override def withOutputPath (x : String ): Config = copy(outputPath = x)
@@ -125,47 +69,15 @@ object FuzzyC2Cpg {
125
69
.text(
126
70
" source file extensions to include when gathering source files. Defaults are .c, .cc, .cpp, .h and .hpp" )
127
71
.action((pat, cfg) => cfg.copy(sourceFileExtensions = cfg.sourceFileExtensions + pat)),
128
- opt[String ](" include" )
129
- .unbounded()
130
- .text(" header include files" )
131
- .action((incl, cfg) => cfg.copy(includeFiles = cfg.includeFiles + incl)),
132
- opt[String ]('I' , " " )
133
- .unbounded()
134
- .text(" header include paths" )
135
- .action((incl, cfg) => cfg.copy(includePaths = cfg.includePaths + incl)),
136
- opt[String ]('D' , " define" )
137
- .unbounded()
138
- .text(" define a name" )
139
- .action((d, cfg) => cfg.copy(defines = cfg.defines + d)),
140
- opt[String ]('U' , " undefine" )
141
- .unbounded()
142
- .text(" undefine a name" )
143
- .action((u, cfg) => cfg.copy(undefines = cfg.undefines + u)),
144
- opt[String ](" preprocessor-executable" )
145
- .text(" path to the preprocessor executable" )
146
- .action((s, cfg) => cfg.copy(preprocessorExecutable = s)),
147
72
)
148
73
}
149
74
150
75
X2Cpg .parseCommandLine(args, frontendSpecificOptions, Config ()) match {
151
76
case Some (config) =>
152
77
try {
153
78
val fuzzyc = new FuzzyC2Cpg ()
154
- if (config.usePreprocessor) {
155
- fuzzyc.runWithPreprocessorAndOutput(
156
- config.inputPaths,
157
- config.sourceFileExtensions,
158
- config.includeFiles,
159
- config.includePaths,
160
- config.defines,
161
- config.undefines,
162
- config.preprocessorExecutable,
163
- Some (config.outputPath)
164
- )
165
- } else {
166
- val cpg = fuzzyc.runAndOutput(config.inputPaths, config.sourceFileExtensions, Some (config.outputPath))
167
- cpg.close()
168
- }
79
+ val cpg = fuzzyc.runAndOutput(config.inputPaths, config.sourceFileExtensions, Some (config.outputPath))
80
+ cpg.close()
169
81
} catch {
170
82
case NonFatal (ex) =>
171
83
logger.error(" Failed to generate CPG." , ex)
0 commit comments