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 16import logging 17 18from pw_presubmit import ( 19 build, 20 Check, 21 format_code, 22 PresubmitContext, 23 filter_paths, 24) 25 26_LOG: logging.Logger = logging.getLogger(__name__) 27 28 29@filter_paths(endswith=format_code.CPP_HEADER_EXTS, exclude=(r'\.pb\.h$',)) 30def pragma_once(ctx: PresubmitContext) -> None: 31 """Presubmit check that ensures all header files contain '#pragma once'.""" 32 33 for path in ctx.paths: 34 _LOG.debug('Checking %s', path) 35 with open(path) as file: 36 for line in file: 37 if line.startswith('#pragma once'): 38 break 39 else: 40 ctx.fail('#pragma once is missing!', path=path) 41 42 43@Check 44def asan(ctx: PresubmitContext) -> None: 45 """Test with the address sanitizer.""" 46 build.gn_gen(ctx) 47 build.ninja(ctx, 'asan') 48 49 50@Check 51def msan(ctx: PresubmitContext) -> None: 52 """Test with the memory sanitizer.""" 53 build.gn_gen(ctx) 54 build.ninja(ctx, 'msan') 55 56 57@Check 58def tsan(ctx: PresubmitContext) -> None: 59 """Test with the thread sanitizer.""" 60 build.gn_gen(ctx) 61 build.ninja(ctx, 'tsan') 62 63 64@Check 65def ubsan(ctx: PresubmitContext) -> None: 66 """Test with the undefined behavior sanitizer.""" 67 build.gn_gen(ctx) 68 build.ninja(ctx, 'ubsan') 69 70 71@Check 72def runtime_sanitizers(ctx: PresubmitContext) -> None: 73 """Test with the address, thread, and undefined behavior sanitizers.""" 74 build.gn_gen(ctx) 75 build.ninja(ctx, 'runtime_sanitizers') 76 77 78def all_sanitizers(): 79 # TODO(b/234876100): msan will not work until the C++ standard library 80 # included in the sysroot has a variant built with msan. 81 return [asan, tsan, ubsan, runtime_sanitizers] 82