Skip to content

Fix switch regex to not match numeric values like -1 inside quoted strings#761

Merged
snehara99 merged 13 commits intomainfrom
copilot/fix-parse-compiler-args-bug
Apr 23, 2026
Merged

Fix switch regex to not match numeric values like -1 inside quoted strings#761
snehara99 merged 13 commits intomainfrom
copilot/fix-parse-compiler-args-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 23, 2026

parseCompilerArgs.bat fails when parsing macro definitions containing numeric expressions like -D'LFS_ASSERT(test)'='do { if(!(test)) {return -1;} } while(0)'. The -1 inside the quoted value was incorrectly matched as a compiler switch.

Root Cause

The regex pattern ([a-zA-Z0-9_]+) in parseAnySwitchFromToolArguments matches any alphanumeric sequence after -, including -1. This splits quoted strings at the wrong boundary.

Fix

Require switch names to start with a letter:

- "([a-zA-Z0-9_]+)"
+ "([a-zA-Z][a-zA-Z0-9_]*)"

This prevents numeric tokens like -1, -2, etc. from being matched as switches while still matching valid switches like -D, -Wall, -std.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] parseCompilerArgs.bat fails if argument contains a space</issue_title>
<issue_description>### Brief Issue Summary

I have a makefile with this line :
-D'LFS_ASSERT(test)'='do { if(!(test)) {return -1;} } while(0)' \

The makefile works and makefile-tools is able to parse it and run make tasks.

But when configuring cpptools to use makefile-tools as a configuration provider, parseCompilerArgs.bat fails :

Error running the compiler args parser script c:\Users\ep.t.fasquelle\.vscode\extensions\ms-vscode.makefile-tools-0.12.17\assets\parseCompilerArgs.bat for regions (   -mcpuDONT_USE_EQUAL_AS_SEPARATORtc37xx -mtc162 -save-tempsDONT_USE_EQUAL_AS_SEPARATORobj              -ffunction-sections -fdata-sections -mpragma-data-sections -mpragma-function-sections -fstrict-volatile-bitfields -fgcse -fgcse-sm -fgcse-lm -fgcse-after-reload -ffast-math -funswitch-loops -fpredictive-commoning -fipa-cp-clone -fpeel-loops -fmove-loop-invariants -funroll-loops -finline-is-always-inline -Wall -Wextra -Wundef -Wshadow -Wdouble-promotion -Werror-implicit-function-declaration -Wuninitialized -Wno-missing-field-initializers -Wno-type-limits -fno-strict-aliasing -fno-builtin -fno-common -O0 -g3 -iquote -maligned-data-sections -Wmissing-prototypes -1;} } while(0)' -c -o  obj/Dal.o ../../SDK/DAL/Dal.c): ' �tait inattendu.

The compiler args parser script 'c:\Users\ep.t.fasquelle\.vscode\extensions\ms-vscode.makefile-tools-0.12.17\assets\parseCompilerArgs.bat' failed with error code    -mcpuDONT_USE_EQUAL_AS_SEPARATORtc37xx -mtc162 -save-tempsDONT_USE_EQUAL_AS_SEPARATORobj              -ffunction-sections -fdata-sections -mpragma-data-sections -mpragma-function-sections -fstrict-volatile-bitfields -fgcse -fgcse-sm -fgcse-lm -fgcse-after-reload -ffast-math -funswitch-loops -fpredictive-commoning -fipa-cp-clone -fpeel-loops -fmove-loop-invariants -funroll-loops -finline-is-always-inline -Wall -Wextra -Wundef -Wshadow -Wdouble-promotion -Werror-implicit-function-declaration -Wuninitialized -Wno-missing-field-initializers -Wno-type-limits -fno-strict-aliasing -fno-builtin -fno-common -O0 -g3 -iquote -maligned-data-sections -Wmissing-prototypes -1;} } while(0)' -c -o  obj/Dal.o ../../SDK/DAL/Dal.c for regions (undefined)
Sending configuration for file c:\Users\ep.t.fasquelle\Documents\dev\<censored>\SDK\DAL\Dal.c -----------------------------------

(I think ' �tait inattendu should be ' was unexpected here in English)

Debug Log

I believe the above should suffice and I would prefer not spend time sanitizing this very long output.
If you believe you really need the information from debug log I will do it.

Additional Information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 23, 2026 20:27
Co-authored-by: snehara99 <113148726+snehara99@users.noreply.github.com>
Co-authored-by: snehara99 <113148726+snehara99@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix parseCompilerArgs.bat failure with space in argument Fix parseCompilerArgs.bat failure when arguments contain spaces in quoted strings Jan 23, 2026
Copilot AI requested a review from snehara99 January 23, 2026 20:30
@snehara99
Copy link
Copy Markdown
Contributor

@copilot I'm getting the following errors now when I run Makefile: Configure:

Saving opened files before build.
Generating configuration cache with command: 'C:\msys64\usr\bin\make.exe --dry-run --keep-going --print-directory'
Writing the dry-run output: c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro\.vscode\dryrun.log
Generating dry-run elapsed time: 0
Preprocessing: "c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro\.vscode\dryrun.log"
Preprocess elapsed time: 0
Parsing for IntelliSense.
Updating the CppTools IntelliSense Configuration Provider.
Parsing dry-run output for CppTools Custom Configuration Provider.
Error running the compiler args parser script c:\Users\snehara\vscode-makefile-tools\assets\parseCompilerArgs.bat for regions ( -1;} } while(0)'DONT_USE_SPACE_AS_SEPARATORmain.cDONT_USE_SPACE_AS_SEPARATOR-oDONT_USE_SPACE_AS_SEPARATORmain): 'DONT_USE_SPACE_AS_SEPARATORmain.cDONT_USE_SPACE_AS_SEPARATOR-oDONT_USE_SPACE_AS_SEPARATORmain) was unexpected at this time.

The compiler args parser script 'c:\Users\snehara\vscode-makefile-tools\assets\parseCompilerArgs.bat' failed with error code 255 for regions ( -1;} } while(0)'DONT_USE_SPACE_AS_SEPARATORmain.cDONT_USE_SPACE_AS_SEPARATOR-oDONT_USE_SPACE_AS_SEPARATORmain)
Sending configuration for file c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro\main.c -----------------------------------
Parsing for IntelliSense elapsed time: 0
Parsing for launch targets.
Found the following 1 new launch targets defined in the makefile: c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro>main()
Complete list of launch targets: c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro>main()
Parsing for launch targets elapsed time: 0
Generating parse content for build targets.
Generating targets information with command: 'C:\msys64\usr\bin\make.exe all --print-data-base --no-builtin-variables --no-builtin-rules --question'
Writing the dry-run output: c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro\.vscode\targets.log
Generating dry-run elapsed time: 0
Parsing for build targets from: "c:\Users\snehara\Downloads\makefile-tools_bug_repro\makefile-tools_bug_repro\.vscode\targets.log"
Found the following 5 new build targets defined in the makefile: all;clean;main;rebuild;run
Complete list of build targets: all;clean;main;rebuild;run
Parsing build targets elapsed time: 0
Configure finished. The status for all the subphases that ran:
generateParseContent: return code = 0, elapsed time = 0
preprocessParseContent: return code = 0, elapsed time = 0
parseIntelliSense: return code = 0, elapsed time = 0
parseLaunch: return code = 0, elapsed time = 0
dryrunTargets: return code = 1, elapsed time = 0
parseTargets: return code = 0, elapsed time = 0
Configure succeeded.
Configure elapsed time: 0
Configuration for file C:\USERS\SNEHARA\DOWNLOADS\MAKEFILE-TOOLS_BUG_REPRO\MAKEFILE-TOOLS_BUG_REPRO\INCLUDED\INCLUDED.H was not found. CppTools will set a default configuration

included.h exists under the included directory too

Co-authored-by: snehara99 <113148726+snehara99@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 10, 2026

@copilot I'm getting the following errors now when I run Makefile: Configure:

...

Fixed in f1de702. The issue was that when compilerArgRegions contains partial/unbalanced quotes (e.g., from a truncated -D define), my function incorrectly treated subsequent content as being "inside quotes" and replaced all spaces.

The fix adds a hasBalancedQuotes() check - if the string has an odd number of single or double quotes, it returns the original string unchanged instead of attempting the replacement. This handles the case where the parsing produces fragments that start mid-quote.

Co-authored-by: snehara99 <113148726+snehara99@users.noreply.github.com>
Co-authored-by: snehara99 <113148726+snehara99@users.noreply.github.com>
Copilot AI changed the title Fix parseCompilerArgs.bat failure when arguments contain spaces in quoted strings Fix switch regex to not match numeric values like -1 inside quoted strings Feb 12, 2026
@snehara99 snehara99 marked this pull request as ready for review April 22, 2026 16:54
snehara99 and others added 5 commits April 22, 2026 13:04
GNU make outputs directory banners like:
  make.exe: Entering directory `path'
The trailing single quote was opening a phantom quote context in
replaceStringNotInQuotes when processing the entire dry-run output
as a single string. This caused semicolons inside quoted macro values
(e.g. -D'MY_ASSERT(test)'='do { return -1; }') to be incorrectly
replaced with newlines, truncating compiler commands.

Fix: process the semicolon and && replacements per-line instead of
on the whole string. After line-continuation joining, each line is a
complete command, so quote state should not leak across lines.

Added regression tests for both semicolon and && cases with
make directory banners.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The regex in parseMultipleSwitchFromToolArguments matched quoted define
names (e.g., 'MY_ASSERT(test)') as complete values, never reaching the
name=value branch. This caused IntelliSense to show only the macro name
without its body.

Changes:
- Add negative lookahead (?!=) after the quoted-value branch so it
  rejects matches followed by '=', falling through to the name=value
  branch that captures both parts
- After outer quote removal, strip shell-style concatenated quoting
  artifacts around '=' (e.g., NAME'='VALUE -> NAME=VALUE)
- Export parseMultipleSwitchFromToolArguments for direct unit testing
- Add 6 unit tests covering quoted name=value, simple defines,
  quoted values with spaces, and multiple defines

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The compilerFragments code naively split the command line on ' -D', which
broke when define values contained spaces (e.g., -D'NAME'='value with
spaces'). This produced truncated fragments that C/C++ extension parsed
as spurious duplicate defines.

Additionally, .split('/s+/') was using a string literal instead of a
regex /\s+/, so it never actually split on whitespace.

Fix: derive compilerFragments from the already-parsed defines array
instead of re-splitting the raw command line. Re-quote values containing
spaces since CppTools shell-parses compilerFragments (per issue #352).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The PR #761 regex change from [a-zA-Z0-9_] to [a-zA-Z] was overly
restrictive — it excluded underscore as a leading character, which
was not the intent. The bug was only about digits (e.g., -1 matching
as a switch). Changed to [a-zA-Z_] to allow underscores while still
rejecting leading digits.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@snehara99
Copy link
Copy Markdown
Contributor

@copilot can you add a changelog entry citing issue #755

@snehara99 snehara99 enabled auto-merge (squash) April 23, 2026 19:18
@snehara99 snehara99 merged commit 72b5ca0 into main Apr 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] parseCompilerArgs.bat fails if argument contains a space

3 participants