From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 8DWmAZM/IWZWyjMAWB0awg (envelope-from ) for ; Thu, 18 Apr 2024 11:43:15 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=MDPka0N7; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id F31D71E0C0; Thu, 18 Apr 2024 11:43:14 -0400 (EDT) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id D831E1E092 for ; Thu, 18 Apr 2024 11:43:12 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B6F73861871 for ; Thu, 18 Apr 2024 15:43:12 +0000 (GMT) Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by sourceware.org (Postfix) with ESMTPS id 7D56A3858D3C for ; Thu, 18 Apr 2024 15:42:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D56A3858D3C 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 7D56A3858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713454926; cv=none; b=qKn98Qs0ya8knQF0x+Av8VG0i9ctPNd0mT4CtKUIzpHHLkcHQe5PMJUa1m/S27Mw5bfoKflfYmlLeBWAWKE7uqY4bO7WCH6+vZSe9uGfWUDwbhNRWh389xLHwkQdUOH5R8eE5ifQ7UTHxp3DzWt3WwVvK3JCGMl3L8c288ArfHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713454926; c=relaxed/simple; bh=izZ6GDTMqR4yMV3t9opzI250hoo88o0oAEgv2j7dtjY=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=EtaOpC23rLwT/O86z7eb/+gv2B+5l8KUAlvVFQ0zTxyoJUssrdez4HotX8Co/ONHVBrnWfpvWH0w/xkeq272k36k7SqCvVeMwZTt8jtG9LYKyuAOAcWPZamUp6AtOAxovrpdc9wWz1BtIMHphkIsfyx1S7Ep1/a8HgQXOM4j3JM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-7d5e19d18fbso30721039f.2 for ; Thu, 18 Apr 2024 08:42:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1713454923; x=1714059723; 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=0EJWgCQFIcGssEpPtMSgOMs5Z5aKSetnazjKMlcvJiQ=; b=MDPka0N7zoV2gnCH9Uv41fXcs7HjW/lQyBpSYIC0OCfwBt5tM2fy7dYNnJ8DGCMlTd /KAFjBvLv7PtIKWtjVqLJxMimdwXa9JhutATXJQ0DYAGhIO9yWpYwziArYqNnM7PgW2d 0YQ9DMzlSu08som/uhrOuZJXMaubBZUoZ6y6eKBkS1GTkP5aU6DlJIieZtbT88yB5FKh x1Xe6egTadfW3skt3QagS7fe/Nnc9OFMdWHhnvddA0Y859k1npjhiI3gIDormABfbnsZ sq/w/4RZUX46GfW9Uk5XuZ8C3TgG8nsnPef/BDbVT7Evf1tmrzLLEbhGayQV9XjugikJ yJ2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713454923; x=1714059723; 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=0EJWgCQFIcGssEpPtMSgOMs5Z5aKSetnazjKMlcvJiQ=; b=CupOgejB7CvkpMzhtqQ+BnRGwvdqxlBR1hlA2XgcUo6Hgq3oNrtQ8wTN9OUq1hc5ZT AqTbLIxC8e6YJY+pg/DILB7R00pXXwS0//OTIs7CP4h2y6fkZ8LZgHam4ykhtOzrYKWE pUaC4XD3mrm4+Z+Ypu9D4WizoQwRlNiSSKeAIXPlZQoJllq54VfnQdN8dCqKpiQF+Crg bUxIHITSJEmlfnCKIWXUF+pHBdW0tVfk9kTqE1NdMEOkj2lVBe9+Be3atelqEHOPsqt2 HByWsGCct0KdH5qqu0lERJGVETriwTjg4a0HHAfh5ZEEgqv5tbrcRLz2SuO7jz90PmLU 0Ebw== X-Gm-Message-State: AOJu0Yx+2yCHQMLg1aM19wdeg+q0LbZWUh14RtBMRHaJrU3rpsiJ1zGl 98cyA4cqYR61nWHT7QzvHvS8k7HDVPfmNz3KdIgqRFWq2h4L1mxCfhLRYAJlXRcZV3VczH/pfVU = X-Google-Smtp-Source: AGHT+IFJDC+nwRm7e6WJ2rPkzAnQ+FBnCIjsHZwhHylPvOwxfwAZJtpIqJcfWGRCZk7O7b7nS/UFFQ== X-Received: by 2002:a5d:9592:0:b0:7d5:d60b:c425 with SMTP id a18-20020a5d9592000000b007d5d60bc425mr3969675ioo.19.1713454923677; Thu, 18 Apr 2024 08:42:03 -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.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 08:42:03 -0700 (PDT) From: Tom Tromey Date: Thu, 18 Apr 2024 09:41:58 -0600 Subject: [PATCH v2 3/4] Add check-include-guards.py to pre-commit MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240418-header-check-v2-3-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, 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+public-inbox=simark.ca@sourceware.org This changes pre-commit to run check-include-guards.py. --- .pre-commit-config.yaml | 7 +++++++ gdb/check-include-guards.py | 35 ++++++++++------------------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8721dac678b..b3dcf6482cf 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,3 +22,10 @@ repos: - id: isort types_or: [file] files: 'gdb/.*\.py(\.in)?$' + - repo: local + hooks: + - id: check-include-guards + name: check-include-guards + language: python + entry: gdb/check-include-guards.py + files: '^(gdb(support|server)?)/[^/]*(/[^/]*)?\.h$' diff --git a/gdb/check-include-guards.py b/gdb/check-include-guards.py index 5c61fc90bb0..428b5a71fa2 100755 --- a/gdb/check-include-guards.py +++ b/gdb/check-include-guards.py @@ -22,27 +22,19 @@ # ./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 failure(filename: str, ndx: int, text: str): + print(filename + ":" + str(ndx + 1) + ": " + text, file=sys.stderr) + sys.exit(1) -def skip_comments_and_blanks(ndx, contents): +def skip_comments_and_blanks(ndx: int, contents: list[str]): while ndx < len(contents) and contents[ndx].startswith("/*"): while ndx < len(contents): ndx += 1 @@ -56,12 +48,12 @@ def skip_comments_and_blanks(ndx, contents): return ndx -def write_header(filename, contents): +def write_header(filename: str, contents: list[str]): with open(filename, "w") as f: f.writelines(contents) -def check_header(filename): +def check_header(filename: str): # Turn x/y-z.h into X_Y_Z_H. assert filename.endswith(".h") expected = filename.replace("-", "_") @@ -70,28 +62,28 @@ def check_header(filename): expected = expected.upper() with open(filename) as f: contents = list(f) + if len(contents) == 0: + # Empty file -- pathological but we can just ignore rather + # than crashing. + return 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" @@ -99,20 +91,15 @@ def check_header(filename): 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: @@ -121,5 +108,3 @@ def check_header(filename): for filename in sys.argv[1:]: check_header(filename) - -sys.exit(status) -- 2.43.0