summaryrefslogtreecommitdiffstats
path: root/src/parser/clang/config.rs
blob: 1d9c6badf6c5f7d4b973ead66fb710b7d11b6b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use anyhow::Result;
use serde_derive::{Deserialize, Serialize};
use structopt::StructOpt;

use std::path::PathBuf;

#[derive(Debug, Clone, StructOpt, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct Config {
    pub(super) compile_commands_location: PathBuf,
    pub(super) extra_args: Vec<String>,
}

#[derive(Debug, Clone, Default, StructOpt, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct ProvidedConfig {
    #[structopt(long = "clang-compile-commands-location")]
    pub(super) compile_commands_location: Option<PathBuf>,
    #[structopt(long = "clang-extra-args", number_of_values = 1)]
    pub(super) extra_args: Option<Vec<String>>,
}

impl Default for Config {
    fn default() -> Self {
        Config::from_merge(ProvidedConfig::default(), ProvidedConfig::default())
            // Currently errors out only on CLI parse fail with clang's extra args
            .unwrap()
    }
}

fn default_extra_args() -> Vec<String> {
    vec![
        // We don't parse function bodies so libclang report every arguments/functions/etc. as unused.
        String::from("-Wno-unused-const-variable"),
        String::from("-Wno-unused-function"),
        String::from("-Wno-unused-parameter"),
        String::from("-Wno-unused-private-field"),
        String::from("-Wno-unused-variable"),
        // We don't "link" so every linker arguments from the compile_commands.json gets reported
        String::from("-Qunused-arguments"),
    ]
}

impl Config {
    pub(crate) fn from_merge(cli: ProvidedConfig, config: ProvidedConfig) -> Result<Self> {
        let mut extra_args = Vec::new();
        if let Some(cli_extra_args) = cli.extra_args {
            for args in cli_extra_args {
                extra_args.append(&mut ::shell_words::split(&args)?);
            }
        }
        extra_args.append(&mut config.extra_args.unwrap_or_else(default_extra_args));
        Ok(Self {
            compile_commands_location: cli
                .compile_commands_location
                .or(config.compile_commands_location)
                .unwrap_or_else(|| PathBuf::from(r".")),
            extra_args,
        })
    }
}