Skip to content

Commit 7bfef75

Browse files
authored
Merge branch 'master' into tests_PcapPlusPlus
2 parents c45152a + 68ed34f commit 7bfef75

File tree

129 files changed

+1958
-379
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1958
-379
lines changed

infra/base-images/base-builder/indexer/clang_wrapper.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import json
2626
import os
2727
from pathlib import Path # pylint: disable=g-importing-member
28+
import shlex
29+
import shutil
2830
import subprocess
2931
import sys
3032
import time
@@ -49,7 +51,6 @@
4951
"-ffile-prefix-map=",
5052
)
5153

52-
PROJECT = Path(os.environ["PROJECT_NAME"])
5354
SRC = Path(os.getenv("SRC", "/src"))
5455
# On OSS-Fuzz build infra, $OUT is not /out.
5556
OUT = Path(os.getenv("OUT", "/out"))
@@ -280,10 +281,6 @@ def read_cdb_fragments(cdb_path: Path) -> Any:
280281

281282
def run_indexer(build_id: str, linker_commands: dict[str, Any]):
282283
"""Run the indexer."""
283-
index_dir = INDEXES_PATH / build_id
284-
# TODO: check if this is correct.
285-
index_dir.mkdir(exist_ok=True)
286-
287284
# Use a build-specific compile commands directory, since there could be
288285
# parallel linking happening at the same time.
289286
compile_commands_dir = INDEXES_PATH / f"compile_commands_{build_id}"
@@ -300,6 +297,15 @@ def run_indexer(build_id: str, linker_commands: dict[str, Any]):
300297
)
301298
return
302299

300+
index_dir = INDEXES_PATH / build_id
301+
if index_dir.exists():
302+
# A previous indexer already ran for the same build ID. Clear the directory
303+
# so we can re-run the indexer, otherwise we might run into various issues
304+
# (e.g. the indexer doesn't like it when source files already exist).
305+
shutil.rmtree(index_dir)
306+
307+
index_dir.mkdir()
308+
303309
with (compile_commands_dir / "compile_commands.json").open("wt") as f:
304310
json.dump(linker_commands["compile_commands"], f, indent=2)
305311

@@ -429,8 +435,36 @@ def _write_filter_log(
429435
f.write(f"\t{cu_path}\n")
430436

431437

438+
def expand_rsp_file(argv: Sequence[str]) -> list[str]:
439+
# https://llvm.org/docs/CommandLine.html#response-files
440+
expanded = []
441+
for arg in argv:
442+
if arg.startswith("@"):
443+
with open(arg[1:], "r") as f:
444+
expanded_args = shlex.split(f.read())
445+
expanded.extend(expanded_args)
446+
else:
447+
expanded.append(arg)
448+
449+
return expanded
450+
451+
452+
def force_optimization_flag(argv: Sequence[str]) -> list[str]:
453+
"""Forces -O0 in the given argument list."""
454+
args = []
455+
for arg in argv:
456+
if arg.startswith("-O") and arg != "-O0":
457+
arg = "-O0"
458+
459+
args.append(arg)
460+
461+
return args
462+
463+
432464
def main(argv: list[str]) -> None:
465+
argv = expand_rsp_file(argv)
433466
argv = remove_flag_if_present(argv, "-gline-tables-only")
467+
argv = force_optimization_flag(argv)
434468

435469
if _has_disallowed_clang_flags(argv):
436470
raise ValueError("Disallowed clang flags found, aborting.")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2025 Google LLC.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""clang_wrapper tests."""
17+
18+
import clang_wrapper
19+
import unittest
20+
21+
22+
class ClangWrapperTest(unittest.TestCase):
23+
24+
def test_force_optimization_flag_no_optimization(self):
25+
"""Tests that optimization flags are not forced when not present."""
26+
argv = ["clang", "-c", "test.c", "-o", "test.o"]
27+
modified_argv = clang_wrapper.force_optimization_flag(argv)
28+
self.assertCountEqual(modified_argv, argv)
29+
30+
def test_force_optimization_flag(self):
31+
"""Tests that optimization flags are forced when present."""
32+
argv = ["clang", "-O2", "-c", "test.c", "-o", "test.o", "-O1"]
33+
modified_argv = clang_wrapper.force_optimization_flag(argv)
34+
self.assertCountEqual(
35+
modified_argv, ["clang", "-O0", "-c", "test.c", "-o", "test.o", "-O0"]
36+
)
37+
38+
39+
if __name__ == "__main__":
40+
unittest.main()

0 commit comments

Comments
 (0)