From patchwork Fri Apr 19 15:03:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 88738 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 12635384772B for ; Fri, 19 Apr 2024 15:06:33 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by sourceware.org (Postfix) with ESMTPS id A455D3849AC5 for ; Fri, 19 Apr 2024 15:04:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A455D3849AC5 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 A455D3849AC5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713539092; cv=none; b=Vc4YdMI1Qwn9XLnIFvRi3P4G+Hz0XMt6nIUKqUDtbj6umZD48bnYQ1f8VwFtMWpsi7/Ch5vRApgbJfeaWp42pfsDETR5vOUOGSBhdOeiAsunXVCIGgIMaRkShSzbKJ63x9MzCmzfwJkZTJPgUHbMUdmfcVAE5C1EZXAxRmzfVWc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713539092; c=relaxed/simple; bh=goXf5zK29sif6uWmvzg6KRlv1W3pmReUlbzb7qNoMQ0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=V+F7W1+DbYPesWHZYsV68zqY6S6ZkC5xAlQcgHm4d1cgxKygO8UoELjb8/mcKr6pn/MPx4KmNIW35ONCPZTKV5tvxsHDPtc3nlBGvt8L9Pkbu64LU+hth5UjDEbyCF0zmirHtENpNsqzj4UzVPwga4FTnG+/xQeytd/9fjsH9Go= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-7da1d81d042so78836639f.0 for ; Fri, 19 Apr 2024 08:04:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1713539085; x=1714143885; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c4ZbLLgcRJjBUZuMAsEVaeKcdf0jN1uIXJurIcT0SYo=; b=Lx5oWOYuZLCA7qRqCEQWBhrMWDkVWQzmhc1aOS+GmNncCaHdYGfLVDWhI4lE3V0lt8 5ZMiB5kTKatmnkwgps9oS0ju2+s6uA5/4D9v/oc8asP5SbR0/n08gwzDKuW5eaoqSdhC n2Fz9mWnmD6+aqmOHa2J9MnWLobOlj9+HdNNA5OqJBph8kXF6aqAi7ad2Np3fZEqs6+V bxKwAeapqpsBb326b9fAE3ZqJlkjPay42jOeIR1/r36DErqwXc+WUADCVb0pYgBlcIfj wz1SqfKseNXMPB/aJaiF0WxEBi/GYP22QWLYIqs+9WggqN8aDPNRzeMEpr/60FWAvj84 sYXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713539085; x=1714143885; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c4ZbLLgcRJjBUZuMAsEVaeKcdf0jN1uIXJurIcT0SYo=; b=HqOSMspZOGtYz8FTmkzPHHbXTrq4cvX6yjH4bueyPOPSUR9C1lMitNtOfH9Q5tMAY0 d9D4tqmdT20WThHs2b6O2iVZGICSj/RodTd0CAhj6J6m35MDFyDLvdGTqbuuxsD72AAK 672GqMfsijX2bGhd3e1QN1CeeyUqWNiE/yromIN9a49gg6v60Z1LsbL4Susaw9+0K5x7 wBJRrBwP/hMmT3kaqZge+WQhSdbJTm3Mm+QB3h4IPfvfhGP6P9kfle8Vx6zxpj9RF0Wr 6yZzVIWsfx8szusM4ef1diZOB3WDpTGUbceTClPvOg0hBtOw8ZdrajRlkF5UVkrd8Gjo OzPA== X-Gm-Message-State: AOJu0YzpE5YBHw8PB9m12jBtm3YcRA1ng8sRc61P0nQzQg7377B/bhD3 ueB+CTfeGS7gT61b6D/Sz17EgiVe6fAq1McKI4gWolvkEbkoIpjznJkCV6e6YsKhkKEHuOCvcss = X-Google-Smtp-Source: AGHT+IHDg4SHCzjFPgaN1r1OqB0YkCRK2fVwcIBBd/59MbpRH/E6vvaM8dRirpIyvwipNVL6bof1eg== X-Received: by 2002:a6b:6008:0:b0:7d0:5735:c460 with SMTP id r8-20020a6b6008000000b007d05735c460mr2559138iog.3.1713539084511; Fri, 19 Apr 2024 08:04:44 -0700 (PDT) Received: from localhost.localdomain (97-122-86-252.hlrn.qwest.net. [97.122.86.252]) by smtp.gmail.com with ESMTPSA id o1-20020a056638124100b0048464427351sm1086506jas.78.2024.04.19.08.04.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 08:04:44 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v4 2/4] Add an include-checking script Date: Fri, 19 Apr 2024 09:03:40 -0600 Message-ID: <20240419150427.897379-3-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240419150427.897379-1-tromey@adacore.com> References: <20240419150427.897379-1-tromey@adacore.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 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(+) create mode 100755 gdb/check-include-guards.py 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)