From patchwork Wed Apr 17 22:05:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 88635 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 A0AE3384AB47 for ; Wed, 17 Apr 2024 22:06:30 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 848223858D3C for ; Wed, 17 Apr 2024 22:05:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 848223858D3C 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 848223858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::135 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713391523; cv=none; b=FnxTp5T+IBCjIenVZWOes2XA3Vbqxdhu3xfecQvYFQMAZCL7BVNQZ1KzXYh0NuT7ljsN+wiwLrpfbpPuf2Ho9xmSIWn2+T5GS/OaJ2t+7iOnWrvm8AzLw9gjcCx+InHDtkJyEoVGzdp7ThwcUZ5x1Xf4DZkgNSLAkK3dUcQad4w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713391523; c=relaxed/simple; bh=XpEfKE1MWUurYXqILnjZHx6W3PIkO6NV1K6buCr9WLA=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=FrTRKKPE6JQp6V6NDkAMrpZOOpv18zODTcr1LiXBcTzACzH1zd15pUJyXVqQPp1Tm44tFTK81k3C0ErC42ldMv5a9VLk6ekg6kboFDU7cjjd9grG4EGAP3BJVcaAFJpekeOthxclxn6VY1bOjbuSLbI1dFxyaJftKBKNwtkwYmI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-36b0ae2b7d7so796455ab.3 for ; Wed, 17 Apr 2024 15:05:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1713391520; x=1713996320; 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=S0zZuUh6CJzfDiHNKyBHlLAXIPbHLNHV0IzalcpCljPC5hStxw6va9GBh3vN1kqkR/ Q2MLOVxzrIo6ltk3HylvL+ydHRKotxQ7y0gAGTkFF6UNi5ZnnnNLNGP2OoM7H/Cwpg25 MM1xTGu8YZD1SrJsdPF0JEGKCneWelWJodSzat/KR+GsO5vUBB5WHBOP4SGRCAwPQ+gI CxIV275E5+2JeJhsWwZ+xxsSR4wu4ruBNnBvQV9DU2XA4Uwx9QShywDl5IGlNFtVpDJc iMee5BfqXeG+U9xMfyw5MAR5A9ktHr9H0az2eQGOaZYWHTtzz68Zit+6FEgxh/lYkq4T cbVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713391520; x=1713996320; 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=Emz7CgmjagAaabelnPXjC5+d1JZqC7sx7Mb/0d/dLAI1LvT5LRtMONNVI53gQJuS0s xrBPDzVRSJPi/wIRVnBaL5inIsnaxpGU27DoUKl7Dwum3gZqQiv6ti9xHefqupDDrTb1 UYjtmjJQAy+V1j4a8NUYB7PY+YF9/TDvltL/jmI+SHHZmz+MgQR5KeYUcU8WXZiUvBdL N6FmDNMRR6FfnRWieQk8f+5AdObJb2+9HqH6U/wsRRJbc/K3NKC5lvcB5ITpuKb/+Pnw yxw4hYPaPjmMuz6Kg1HzQ59yfwxPAD90Cef4ZrurV1ttrVrHyvTxkWtvmTwNz1Csn4MH Zazw== X-Gm-Message-State: AOJu0YyNuVQ6C+H77Fs3z4KzqD4BfwXyXfj1EgyivfWA6wOeIY1Wg1hD 7oy5LgIVcGnGkGu3k+o0RXba4x4OkVNUb5Hp6RXLdZrOrsFxoYW1qM8Erx5HMotl2NkL1K/CxjM = X-Google-Smtp-Source: AGHT+IGHkvUZfojaA/jfqQFZDsspE1kIDRhI/BtIb1EZ0AP4ukpf0UsPxUcf7geRQGx65cp66jgiDg== X-Received: by 2002:a05:6e02:13af:b0:36a:2538:d70d with SMTP id h15-20020a056e0213af00b0036a2538d70dmr1045161ilo.28.1713391520636; Wed, 17 Apr 2024 15:05:20 -0700 (PDT) Received: from localhost.localdomain (97-122-82-115.hlrn.qwest.net. [97.122.82.115]) by smtp.gmail.com with ESMTPSA id 3-20020a056e020ca300b0036b2fce985dsm22985ilg.62.2024.04.17.15.05.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Apr 2024 15:05:20 -0700 (PDT) From: Tom Tromey Date: Wed, 17 Apr 2024 16:05:06 -0600 Subject: [PATCH 2/4] Add an include-checking script MIME-Version: 1.0 Message-Id: <20240417-header-check-v1-2-a60f610f5bcc@adacore.com> References: <20240417-header-check-v1-0-a60f610f5bcc@adacore.com> In-Reply-To: <20240417-header-check-v1-0-a60f610f5bcc@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.13.0 X-Spam-Status: No, score=-11.3 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. Tested-By: Tom de Vries --- 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)