Skip to content

Commit 9400824

Browse files
committedJan 11, 2025
Support script file mode
1 parent 850f64f commit 9400824

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎src/arguments.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl Arguments {
2727
pub enum Command {
2828
ReplMode(Arguments),
2929
QueryMode(String, Arguments),
30+
ScriptMode(String, Arguments),
3031
Help,
3132
Version,
3233
Error(String),
@@ -44,6 +45,7 @@ pub fn parse_arguments(args: &[String]) -> Command {
4445
}
4546

4647
let mut optional_query: Option<String> = None;
48+
let mut optional_script_file: Option<String> = None;
4749
let mut arguments = Arguments::new();
4850

4951
let mut arg_index = 1;
@@ -95,6 +97,16 @@ pub fn parse_arguments(args: &[String]) -> Command {
9597
optional_query = Some(args[arg_index].to_string());
9698
arg_index += 1;
9799
}
100+
"--script" | "-s" => {
101+
arg_index += 1;
102+
if arg_index >= args_len {
103+
let message = format!("Argument {} must be followed by the file", arg);
104+
return Command::Error(message);
105+
}
106+
107+
optional_script_file = Some(args[arg_index].to_string());
108+
arg_index += 1;
109+
}
98110
"--analysis" | "-a" => {
99111
arguments.analysis = true;
100112
arg_index += 1;
@@ -147,7 +159,9 @@ pub fn parse_arguments(args: &[String]) -> Command {
147159
return Command::Error("Must provide one or more C/C++ files".to_string());
148160
}
149161

150-
if let Some(query) = optional_query {
162+
if let Some(script_file) = optional_script_file {
163+
Command::ScriptMode(script_file, arguments)
164+
} else if let Some(query) = optional_query {
151165
Command::QueryMode(query, arguments)
152166
} else {
153167
Command::ReplMode(arguments)

‎src/main.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fs;
12
use std::io::IsTerminal;
23
use std::path::Path;
34

@@ -34,6 +35,23 @@ fn main() {
3435
let command = arguments::parse_arguments(&args);
3536
match command {
3637
Command::ReplMode(arguments) => launch_clangql_repl(arguments),
38+
Command::ScriptMode(script_file, arguments) => {
39+
let mut reporter = DiagnosticReporter::default();
40+
let files = &arguments.files;
41+
if let Err(error) = validate_files_paths(files) {
42+
reporter.report_diagnostic("", Diagnostic::error(error.as_str()));
43+
return;
44+
}
45+
46+
let mut env = create_clang_ql_environment();
47+
let query =
48+
fs::read_to_string(script_file).expect("Should have been able to read the file");
49+
50+
let compilation_units = parse_files(files);
51+
let provider: Box<dyn DataProvider> =
52+
Box::new(ClangDataProvider::new(compilation_units));
53+
execute_clang_ql_query(query, &arguments, &mut env, &provider, &mut reporter);
54+
}
3755
Command::QueryMode(query, arguments) => {
3856
let mut reporter = DiagnosticReporter::default();
3957
let files = &arguments.files;

0 commit comments

Comments
 (0)
Please sign in to comment.