1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""C++-related checks.""" 15 16from pw_presubmit import ( 17 build, 18 Check, 19 format_code, 20 PresubmitContext, 21 PresubmitFailure, 22 filter_paths, 23) 24 25 26@filter_paths(endswith=format_code.CPP_HEADER_EXTS, exclude=(r'\.pb\.h$', )) 27def pragma_once(ctx: PresubmitContext) -> None: 28 """Presubmit check that ensures all header files contain '#pragma once'.""" 29 30 for path in ctx.paths: 31 with open(path) as file: 32 for line in file: 33 if line.startswith('#pragma once'): 34 break 35 else: 36 raise PresubmitFailure('#pragma once is missing!', path=path) 37 38 39@Check 40def asan(ctx: PresubmitContext) -> None: 41 build.gn_gen(ctx.root, ctx.output_dir) 42 build.ninja(ctx.output_dir, 'asan') 43 44 45@Check 46def msan(ctx: PresubmitContext) -> None: 47 build.gn_gen(ctx.root, ctx.output_dir) 48 build.ninja(ctx.output_dir, 'msan') 49 50 51@Check 52def tsan(ctx: PresubmitContext) -> None: 53 build.gn_gen(ctx.root, ctx.output_dir) 54 build.ninja(ctx.output_dir, 'tsan') 55 56 57@Check 58def ubsan(ctx: PresubmitContext) -> None: 59 build.gn_gen(ctx.root, ctx.output_dir) 60 build.ninja(ctx.output_dir, 'ubsan') 61 62 63@Check 64def runtime_sanitizers(ctx: PresubmitContext) -> None: 65 build.gn_gen(ctx.root, ctx.output_dir) 66 build.ninja(ctx.output_dir, 'runtime_sanitizers') 67 68 69def all_sanitizers(): 70 return [asan, msan, tsan, ubsan, runtime_sanitizers] 71