Skip to content

Commit

Permalink
SQF: Some Basic Lints (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson authored Jan 25, 2024
1 parent eb09e37 commit d292c35
Show file tree
Hide file tree
Showing 40 changed files with 1,488 additions and 198 deletions.
193 changes: 86 additions & 107 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ members = [
resolver = "2"

[workspace.dependencies]
ariadne = "0.3.0"
# ariadne = "0.3.0"
ariadne = { git = "https://github.com/BrettMayson/ariadne" }
byteorder = "1.5.0"
chumsky = "0.9.3"
clap = "4.4.11"
Expand Down
2 changes: 1 addition & 1 deletion bin/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn executor(ctx: Context, matches: &ArgMatches) -> Executor {

executor.init();
executor.check();
executor.build();
executor.build(true);

executor
}
43 changes: 43 additions & 0 deletions bin/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use clap::Command;

use crate::{
context::Context,
error::Error,
executor::Executor,
modules::{pbo::Collapse, Hooks, Rapifier, SQFCompiler},
report::Report,
};

#[must_use]
pub fn cli() -> Command {
Command::new("check").about("Check the project for errors")
}

/// Execute the dev command
///
/// # Errors
/// [`Error`] depending on the modules
pub fn execute() -> Result<Report, Error> {
let ctx = Context::new(
std::env::current_dir()?,
"check",
crate::context::PreservePrevious::Remove,
true,
)?;

let mut executor = Executor::new(ctx);

executor.collapse(Collapse::Yes);

executor.add_module(Box::<Hooks>::default());
executor.add_module(Box::<Rapifier>::default());
executor.add_module(Box::<SQFCompiler>::default());

info!("Running checks");

executor.init();
executor.check();
executor.build(false);

executor.run()
}
2 changes: 1 addition & 1 deletion bin/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn context(matches: &ArgMatches, launch_optionals: &[String]) -> Result<Exec

executor.init();
executor.check();
executor.build();
executor.build(true);

if !just.is_empty() {
warn!("Use of `--just` is not recommended, only use it if you know what you're doing");
Expand Down
1 change: 1 addition & 0 deletions bin/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod build;
pub mod check;
pub mod dev;
pub mod launch;
pub mod new;
Expand Down
8 changes: 5 additions & 3 deletions bin/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ impl Executor {
}

/// The exeuctor will run the `build` phases
pub fn build(&mut self) {
pub fn build(&mut self, write: bool) {
self.stages.push("pre_build");
self.stages.push("build");
self.stages.push("post_build");
if write {
self.stages.push("build");
self.stages.push("post_build");
}
}

/// The exeuctor will run the `release` phases
Expand Down
4 changes: 3 additions & 1 deletion bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ pub fn cli() -> Command {
.subcommand_required(false)
.arg_required_else_help(true)
.subcommand(commands::new::cli())
.subcommand(commands::check::cli())
.subcommand(commands::dev::cli())
.subcommand(commands::build::cli())
.subcommand(commands::launch::cli())
.subcommand(commands::build::cli())
.subcommand(commands::release::cli())
.subcommand(commands::script::cli())
.subcommand(commands::utils::cli())
Expand Down Expand Up @@ -113,6 +114,7 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Error> {
let report = match matches.subcommand() {
Some(("new", matches)) => commands::new::execute(matches).map(Some),
Some(("dev", matches)) => commands::dev::execute(matches, &[]).map(Some),
Some(("check", _matches)) => commands::check::execute().map(Some),
Some(("build", matches)) => commands::build::execute(matches)
.map_err(std::convert::Into::into)
.map(Some),
Expand Down
4 changes: 1 addition & 3 deletions bin/src/modules/sqf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Module for SQFCompiler {
Ok(sqf) => {
let mut out = entry.with_extension("sqfc")?.create_file()?;
let (warnings, errors) =
analyze(&sqf, Some(ctx.config()), &processed, addon, &database);
analyze(&sqf, Some(ctx.config()), &processed, Some(addon), &database);
for warning in warnings {
report.warn(warning);
}
Expand All @@ -63,7 +63,6 @@ impl Module for SQFCompiler {
Ok(report)
}
Err(ParserError::ParsingError(e)) => {
let mut report = Report::new();
if processed.as_str().starts_with("force ")
|| processed.as_str().contains("\nforce ")
{
Expand All @@ -76,7 +75,6 @@ impl Module for SQFCompiler {
Ok(report)
}
Err(ParserError::LexingError(e)) => {
let mut report = Report::new();
for error in e {
report.error(error);
}
Expand Down
6 changes: 3 additions & 3 deletions book/analysis/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ HEMTT provides a few preprocessor flags to control the behavior of the preproces

| Flag | Description |
| ---- | ----------- |
| pw3_ignore_arr | Ignore padded arguments in `ARR_N` macros |
| pw3_ignore_format | Ignores padded arguments in `ARR_N`, `WARNING_N`, `TRACE_N`, `FORMAT_N`, etc. macros |
| pe23_ignore_has_include| Assume any `#if __has_include` is false |

The scope of these flags is the same as the warning suppression scope.
Expand Down Expand Up @@ -72,10 +72,10 @@ HELLO(Jim, Bob)
This would produce `Jim, meet Bob` instead of `Jim, meet Bob`. (Note the extra space before `Bob`).
By default, all macros are checked, but a flag can be set to ignore `ARR_N` macros.
By default, all macros are checked, but a flag can be set to ignore `ARR_N`, `WARNING_N`, `TRACE_N`, `FORMAT_N`, etc. macros.
```cpp
#pragma hemtt flag pw3_ignore_arr { scope = line }
#pragma hemtt flag pw3_ignore_format { scope = line }
```

## Rapify Warnings
Expand Down
5 changes: 4 additions & 1 deletion libs/preprocessor/src/codes/pw3_padded_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use hemtt_common::reporting::{Annotation, AnnotationLevel, Code, Token};
pub struct PaddedArg {
/// The [`Token`] that was found to be padding an arg
token: Box<Token>,
debug: String,
/// The report
report: Option<String>,
}
Expand Down Expand Up @@ -41,9 +42,10 @@ impl Code for PaddedArg {
}

impl PaddedArg {
pub fn new(token: Box<Token>) -> Self {
pub fn new(token: Box<Token>, debug: String) -> Self {
Self {
token,
debug,
report: None,
}
.report_generate()
Expand All @@ -69,6 +71,7 @@ impl PaddedArg {
.with_color(color_token)
.with_message("padding a macro argument is likely unintended"),
)
.with_note(format!("Occured in: `{}`", self.debug))
.finish()
.write_for_stdout(
sources(vec![(
Expand Down
20 changes: 15 additions & 5 deletions libs/preprocessor/src/processor/defines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl Processor {
body
}

#[allow(clippy::too_many_lines)]
/// A define was used
///
/// Expects the stream to be at the ident
Expand Down Expand Up @@ -219,14 +220,23 @@ impl Processor {
let mut arg_defines = HashMap::new();
for (arg, value) in function.args().iter().zip(args) {
if !pragma.is_suppressed(&Suppress::Pw3PaddedArg)
&& (!pragma.is_flagged(&Flag::Pw3IgnoreArr)
|| !ident_string.starts_with("ARR_"))
&& (!pragma.is_flagged(&Flag::Pw3IgnoreFormat) || {
[
"ARR_", "TRACE_", "INFO_", "WARNING_", "ERROR_", "DEBUG_",
"FORMAT_",
]
.iter()
.all(|s| !ident_string.starts_with(s))
})
{
for token in [value.first(), value.last()] {
if token.map_or(false, |t| t.symbol().is_whitespace()) {
self.warnings.push(Arc::new(PaddedArg::new(Box::new(
(**token.expect("token exists from map_or check")).clone(),
))));
self.warnings.push(Arc::new(PaddedArg::new(
Box::new(
(**token.expect("token exists from map_or check")).clone(),
),
ident_string.clone(),
)));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions libs/preprocessor/src/processor/pragma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ impl TryFrom<&str> for Scope {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Flag {
Pw3IgnoreArr,
Pw3IgnoreFormat,
Pe23IgnoreIfHasInclude,
}

impl Flag {
pub const fn as_slice() -> &'static [&'static str] {
&["pw3_ignore_arr", "pe23_ignore_has_include"]
&["pw3_ignore_format", "pe23_ignore_has_include"]
}
}

Expand All @@ -128,7 +128,7 @@ impl TryFrom<&str> for Flag {

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"pw3_ignore_arr" => Ok(Self::Pw3IgnoreArr),
"pw3_ignore_format" => Ok(Self::Pw3IgnoreFormat),
"pe23_ignore_has_include" => Ok(Self::Pe23IgnoreIfHasInclude),
_ => Err(()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
╭─[/source.hpp:2:11]
│
2 │ #include <something.hpp>
 │ ─────────────
 │
───╯
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
╭─[/source.hpp:1:12]
│
1 │ #define HI(var1 var2) Hello var1 var2
 │ ────┬────
 │ │
 │ │
 │ ╰────── multiple tokens found without a comma
 │
 │
 │
 │ Help: try `var1, var2`
───╯
2 changes: 2 additions & 0 deletions libs/preprocessor/tests/warnings/pw3_padded_arg/stderr.ansi
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
4 │ value2 = HELLO(John, Smith);
 │ ┬
 │ ╰── padding a macro argument is likely unintended
 │
 │ Note: Occured in: `HELLO`
───╯
6 changes: 6 additions & 0 deletions libs/sqf/src/analyze/codes/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
pub mod sae1_require_version;

pub mod saa1_if_assign;
pub mod saa2_find_in_str;
pub mod saa3_typename;
pub mod saa4_str_format;
pub mod saa5_select_parse_number;
Loading

0 comments on commit d292c35

Please sign in to comment.