From patchwork Thu Apr 18 15:41:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 88665 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 5C54F384AB61 for ; Thu, 18 Apr 2024 15:42:36 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by sourceware.org (Postfix) with ESMTPS id 5BB403858D39 for ; Thu, 18 Apr 2024 15:42:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5BB403858D39 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 5BB403858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713454925; cv=none; b=TT/km1pev+hAmX757X5UZniRMWr0WC5GEOuzaeDhp/5Z9ONzoiiaBySjNwt5A9t+Gp4Cx6G1PvdGFUGCv7PGqPH/TXBwhJ397GgDJ+rrH0cYXOTz9ramwfF3ue1kAY9ChIWn8HMIh1e7hp7cTs77eJUl4cNuZP5r+0cGSZmlU5g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713454925; c=relaxed/simple; bh=XpEfKE1MWUurYXqILnjZHx6W3PIkO6NV1K6buCr9WLA=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=mr72oXpA1ug1b5lsH7gJSA/pPeiYfGVSbySo/5DL0wHf1fmiqSFxW9lq5TPIwOobJInzqW9LKBXHMUXq9Cbu5dbHcppUNIp9l87tj1JOk7QI7FIGwXKMM/hQcM4l+ufQ79YKFfxwjNq19nQAlqXiusFTnfNluYavm2KeK6gqGI4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd32.google.com with SMTP id ca18e2360f4ac-7d0262036afso37090539f.3 for ; Thu, 18 Apr 2024 08:42:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1713454922; x=1714059722; 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=DF7BiEMatMIqDtZAJUnK8OrpDJC/qAn7HgdchLk1IgtKN1xn5q4IkMrmiHY++ZT+/C O1RgaytQ4xlj72+CgS7IVUxjj8WHB5THqKFbfJ84FhtQJn0rGVTowgwLPO4fnv7tD5hV UOShZXt8IKHulxDIjamRi7YGFg6fZsyKn+Xh6OVPCQ3wqUIXy+21ImA7Ehr27TQMGhVl c7ISMvg0g36FMUtEXW6RaPBsZsA+BpfiXGNM4KQuI6cS39vkEbUbBucXJ6SW4m0ICoPX BQzHmnxWCfLWBMWSfIpehNzuP8Z/rJrR6di548Q4k49t0EHO/P0uGYhaphEwMFs/eF0l LKtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713454922; x=1714059722; 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=snmXNbsOLQIn1xG5Ijr/Eo4m2oBwjSAHMYIrhIWDyrMS6o4PqGPxedyRrM9+YdsIdl CcBwE48HjR2h/QuJJ2EjD0BUtvfWNeEV0VcERI/A/8PEME3mlV1xVYmCO3Zl11bqKCMB odp3yCptkJFIFDrwZVDqqCCS5g8johAYQKf59WrXtwR7jdWvXaZiWP+/jcu8X10LtWgw ny2S4GP7UDWoUGjqdEH5RHzw4VD38x1K09reqRHVEwc4yiVHNQmStJEFpETqkgCW7EYO 3RQMpFAJkKIPrYY035bqyjHP5W97Cxj1Krm/Jp7owxGecntXdX1xp2DPcVXJYw8IIcAV KDVA== X-Gm-Message-State: AOJu0Ywe72hSOutrnMV8wLK4YxVTDr7OaY3lj3ODpX525D0+hXZ9pQgG IlBcwE469hy4kpYXYaz1hm0dSNDsM6bWrf0owH9548Nj7776UhOrrCmZ4wvCyUt0JevzSksSC8s = X-Google-Smtp-Source: AGHT+IEpZ5Pxe6DBNAN4Dr1L6ymQYBAQ3O0+P8hy5KMcdWNGxYvFHd55onk/HAi/UPHb9S7sUdJphQ== X-Received: by 2002:a5e:9e47:0:b0:7d6:3fab:85bd with SMTP id j7-20020a5e9e47000000b007d63fab85bdmr3843273ioq.5.1713454922297; Thu, 18 Apr 2024 08:42:02 -0700 (PDT) Received: from localhost.localdomain (97-122-82-115.hlrn.qwest.net. [97.122.82.115]) by smtp.gmail.com with ESMTPSA id f5-20020a05660215c500b007d5e449ad60sm397412iow.22.2024.04.18.08.42.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 08:42:01 -0700 (PDT) From: Tom Tromey Date: Thu, 18 Apr 2024 09:41:57 -0600 Subject: [PATCH v2 2/4] Add an include-checking script MIME-Version: 1.0 Message-Id: <20240418-header-check-v2-2-725529851603@adacore.com> References: <20240418-header-check-v2-0-725529851603@adacore.com> In-Reply-To: <20240418-header-check-v2-0-725529851603@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. --- 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)