Skip to content

Commit e288a54

Browse files
committed
Fix: Handle #pragma GCC target() in C++ bundler
When GCC preprocesses with -dD flag and encounters #pragma GCC target(), it emits extra macro definitions for compiler features (__AVX2__, __SSE4_1__, etc). This causes a line count mismatch in the bundler's assertion that line counts must be equal between raw source and uncommented code. Solution: Temporarily comment out #pragma GCC target() before preprocessing, then uncomment in the output. Uses OJ_BUNDLE_PRAGMA marker to distinguish from user-written comments. Fixes #438
1 parent adbff12 commit e288a54

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

onlinejudge_verify/languages/cplusplus_bundle.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,17 @@ def _get_uncommented_code(path: pathlib.Path, *, iquotes_options: Tuple[str, ...
184184
if compiler == 'g++':
185185
raise BundleError(f'A fake g++ is detected. Please install the GNU C++ compiler.: {compiler}')
186186
raise BundleError(f"It's not g++. Please specify g++ with $CXX envvar.: {compiler}")
187-
command = [compiler, '-x', 'c++', *iquotes_options, '-fpreprocessed', '-dD', '-E', str(path)]
188-
return subprocess.check_output(command)
187+
188+
# Comment out #pragma GCC target() before preprocessing to avoid GCC emitting extra macros
189+
with open(str(path), 'rb') as f:
190+
source = f.read()
191+
source = re.sub(rb'^(\s*)#\s*pragma\s+GCC\s+target\s*\(', rb'\1/* OJ_BUNDLE_PRAGMA */ // #pragma GCC target(', source, flags=re.MULTILINE)
192+
193+
command = [compiler, '-x', 'c++', *iquotes_options, '-fpreprocessed', '-dD', '-E', '-']
194+
result = subprocess.check_output(command, input=source)
195+
196+
# Uncomment pragmas in output
197+
return re.sub(rb'^(\s*)/\*\s*OJ_BUNDLE_PRAGMA\s*\*/\s*//\s*#\s*pragma\s+GCC\s+target\s*\(', rb'\1#pragma GCC target(', result, flags=re.MULTILINE)
189198

190199

191200
def get_uncommented_code(path: pathlib.Path, *, iquotes: List[pathlib.Path], compiler: str) -> bytes:

0 commit comments

Comments
 (0)