From patchwork Fri Apr 19 14:55:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 88733 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 803C23846422 for ; Fri, 19 Apr 2024 14:56:03 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 4150D3849AC5 for ; Fri, 19 Apr 2024 14:55:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4150D3849AC5 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4150D3849AC5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::12c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713538510; cv=none; b=NjZoS4c1Bd8D46X/8TSNr8hPv8L05a1ti+Quj+ch7wyiD7GpnwEcDBZ3L8Ro8d4UfjPp8+Gjb2IfoNBMCgO9SX8Sf55WW9fvG6hOUoMEniHrb90vzwLYolIrfrdvYJunrEvCFNROmk3ZMhHRlNkj/8C06tw+OkQWrbfVvauBZLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713538510; c=relaxed/simple; bh=XpEfKE1MWUurYXqILnjZHx6W3PIkO6NV1K6buCr9WLA=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=gci4QVwpaRu+2oaUFmDVNVTcAWBYSc6SQJqneYQsld2YEffo81tbbfeIUqwOwAK6VA7zRTdJlBwRQxci73O3i9i+bUA0OQKgCYMB6KhETENXUliRjrYd5DE80Q0fa6KWIRxTnavP6JkpReuOKLaLF9cKMFewIr4lJtcznJfXkqQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x12c.google.com with SMTP id e9e14a558f8ab-36b2adedec7so8084225ab.1 for ; Fri, 19 Apr 2024 07:55:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1713538505; x=1714143305; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=MzFXyTyxa5p+qwWEUnW3jpqQyNJt4nZn9AyaMtw918M=; b=S3Zo5yTZUpeGIGKqCkQyn4RakTc+Y6GdcoK6to2qrzJLAL1DzzBtrVpnfyQWyWhT3p r6AOHhzYHowxY9SBuNY11aPzyMB3F3Vgkap2WsUkmO3jk9cLzYyEzE4wHviyhxdnWLXU ZsvP79AiTRB+yCZDpz4xVE9xw8HiwFRzX8TKnqZSHVpMnAsrUMDcYMPm74EgC1x30274 LPKwDM5wPBExUDWwOuCmir/byuud8aWYGb65DBZm/NElhG9JvTP/WcmKQ+cQujzi+mDM mEQbmI8TAHOsQnagk2wO00bwecBmD9QdvYfidn2dx8yJ8Kic4x9xzlKnNcetXV4G7VZr CdZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713538505; x=1714143305; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MzFXyTyxa5p+qwWEUnW3jpqQyNJt4nZn9AyaMtw918M=; b=htGuicUAS1Jl9ZktX3KgUNLibEd7s5EeMu/i7utgI5l7YnuaQRKNUkGtzVzOR3QJdA utyxlg21f0qHQSfb89gge3HHMlN+5vZCYHMCH8oFHYT7qvAQ2XsayLbkG2ZUEzxmBCs5 xaFsgvGMgPA7iyoHb8i1kcQ7u4OuUCjpaoJ20eHEwoouNCBsDop8p/EG/r/0dPyXTyS3 SgybSD/bRUwiqWTwBaqSaYZ3utFwiCyDcj4x0K5MQNGNN40bq+S1l0ltjvAKp5+TlIPR frLsgnBpI/7U4JetvaOXVm13VL66VCvcc0dbz49uiOA24E0tIHC/Uc4qN6XC0umt0IRS PRuQ== X-Gm-Message-State: AOJu0YwBn/Z88nV81NzXdwOI4WW7qlE7LsgC0ntNRPDGd0OMua/UR/Do TA5URIXccpCVBSFpDy2+BBIcJfF+lGo907Z4T1/Y7jW3RoGsRTMNliOLXXH5XrpadOSKn6sQquY = X-Google-Smtp-Source: AGHT+IGt4clYttsnSae19kdRfPjVfriiv4ziHq8sLxN2/WJTrYX6G6j+1DrAuJu+I14dxCr/M/Uc3Q== X-Received: by 2002:a05:6e02:2141:b0:36a:273f:7e03 with SMTP id d1-20020a056e02214100b0036a273f7e03mr3137128ilv.12.1713538505348; Fri, 19 Apr 2024 07:55:05 -0700 (PDT) Received: from localhost.localdomain (97-122-86-252.hlrn.qwest.net. [97.122.86.252]) by smtp.gmail.com with ESMTPSA id u14-20020a056e02110e00b0036b1aa58c5dsm828448ilk.15.2024.04.19.07.55.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 07:55:05 -0700 (PDT) From: Tom Tromey Date: Fri, 19 Apr 2024 08:55:03 -0600 Subject: [PATCH v3 2/4] Add an include-checking script MIME-Version: 1.0 Message-Id: <20240419-header-check-v3-2-2f2a837c2976@adacore.com> References: <20240419-header-check-v3-0-2f2a837c2976@adacore.com> In-Reply-To: <20240419-header-check-v3-0-2f2a837c2976@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.13.0 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This adds a new Python script that checks the header guards of all gdb source files. It enforces a fairly strict formatting and naming scheme. In particular, for a file "x/y-z.h" (relative to the repository root), the include guard will be named "X_Y_Z_H". Only the '#ifndef' form is allowed, not "#if !defined(...)". The trailing comment on the "#endif" is also required. The script also tries to update files that appear to have the required lines if they are in the wrong form or use the wrong name. --- gdb/check-include-guards.py | 125 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/gdb/check-include-guards.py b/gdb/check-include-guards.py new file mode 100755 index 00000000000..5c61fc90bb0 --- /dev/null +++ b/gdb/check-include-guards.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2024 Free Software Foundation, Inc. +# +# This file is part of GDB. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This is intended to be run from pre-commit. You can also run it by +# hand by passing repository-relative filenames to it, like: +# ./gdb/check-include-guards.py gdb/*.h + + +import glob +import re +import sys + +status = 0 + +DEF = re.compile("^#ifndef ([A-Za-z0-9_]+)\n") +OLDDEF = re.compile("^#if !defined *\\(([A-Za-z0-9_]+)\\)\n") + + +def failure(filename, ndx, text): + print(filename + ":" + str(ndx + 1) + ": " + text) + global status + status = 1 + + +def headers(dirname): + return glob.iglob(dirname + "/*.h") + + +def skip_comments_and_blanks(ndx, contents): + while ndx < len(contents) and contents[ndx].startswith("/*"): + while ndx < len(contents): + ndx += 1 + if contents[ndx - 1].endswith("*/\n"): + break + # Skip blank lines. + while ndx < len(contents): + if contents[ndx].strip() != "": + break + ndx += 1 + return ndx + + +def write_header(filename, contents): + with open(filename, "w") as f: + f.writelines(contents) + + +def check_header(filename): + # Turn x/y-z.h into X_Y_Z_H. + assert filename.endswith(".h") + expected = filename.replace("-", "_") + expected = expected.replace(".", "_") + expected = expected.replace("/", "_") + expected = expected.upper() + with open(filename) as f: + contents = list(f) + if "THIS FILE IS GENERATED" in contents[0]: + # Ignore. + return + if not contents[0].startswith("/*"): + failure(filename, 0, "header should start with comment") + return + i = skip_comments_and_blanks(0, contents) + if i == len(contents): + failure(filename, i, "unterminated intro comment or missing body") + return + m = DEF.match(contents[i]) + force_rewrite = False + if not m: + m = OLDDEF.match(contents[i]) + if not m: + failure(filename, i, "no header guard") + return + force_rewrite = True + symbol = m.group(1) + updated = False + if symbol != expected: + failure(filename, i, "symbol should be: " + expected) + force_rewrite = True + if force_rewrite: + contents[i] = "#ifndef " + expected + "\n" + updated = True + i += 1 + if i == len(contents): + failure(filename, i, "premature EOF") + return + if not contents[i].startswith("#define "): + failure(filename, i, "no define of header guard") + return + if contents[i] != "#define " + expected + "\n": + failure(filename, i, "wrong symbol name in define") + contents[i] = "#define " + expected + "\n" + updated = True + i = len(contents) - 1 + if not contents[i].startswith("#endif"): + failure(filename, i, "no trailing endif") + return + if contents[i] != "#endif /* " + expected + " */\n": + failure(filename, i, "incorrect endif") + contents[i] = "#endif /* " + expected + " */\n" + updated = True + if updated: + write_header(filename, contents) + + +for filename in sys.argv[1:]: + check_header(filename) + +sys.exit(status)