From patchwork Sun Oct 8 22:58:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 149779 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1559727vqo; Sun, 8 Oct 2023 15:59:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHlhN7LzzLCeM5KBhkr2EBU5A0Wb0zdpuJ7W1hTHLx+mHILOXeyi0i8EyuFD1CH2I21kmCu X-Received: by 2002:a05:6402:1219:b0:530:77e6:849f with SMTP id c25-20020a056402121900b0053077e6849fmr12046750edw.27.1696805997766; Sun, 08 Oct 2023 15:59:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696805997; cv=none; d=google.com; s=arc-20160816; b=mPwgcR6tXvcSxAEO+owLF0iwEQ4u42x4Lgx/z2l4H6NIDmhSX0v1quQGL0XgstV7G4 VigWUAv2e3fL/SKbg6Sjrje0Ipbsu6poqCLBcv6XFt7Uoyw6Yai0h+6pp1AeKWDXmYaF vJdQ/KTXMbrphtd71/DjkZSAG2joW8H6Qp3HHDoDNSm7a21a2awdTcn9BHH9g4Ucd5pX jFinwAwNU+7gtpEQDXyHkbiX+FkOxGVqkvjpIUrsLoSfUgYMDAgkSv8CV8miP8s+7jdg 7gx5Oi1y0UcoNRCYumIk4BdawTF6c9e6jTileCik8WA4tKBLCqGMEtZcpXUjNBYIWaxN URyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=Bwe+g07rK1rvEaZfgJmhEpYuOpi2FCnXMBwagv5Cbh4=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=F1yWZiBisklGmcoOpPxhRRBfZ2xyVbNC0MEnlRp7xYCaNr4TBk35biaYvspAWrdzX6 dJjJ3A/gGhZm6ee4RtUkM96WV2WsvIW0j+wIuXRYRGjvA2YBo51cW2XC8OcRByKSpoIk b97Zgg58ieqJPK3HUUzGwAdpSdtd8PiIKqdy7sBPPwAe+7I8l/rADHAfX9Uj27IpgWRf VFnCMmQvTjnZyLaQyP1XHIyaEYgXRPbBhmBbI9ayFWGR466WBYdTJwvbOEjwDRnohCed wExfekT39vKicFHvUuQyDpaMW0pKla7KiMh/Ya5vL/tLffJGxLYi9kRicAaLOjhRLTL3 8HQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ODbfSDtp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id f21-20020a056402151500b00537f053642asi3888742edw.44.2023.10.08.15.59.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 15:59:57 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ODbfSDtp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 01CB4385F009 for ; Sun, 8 Oct 2023 22:59:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 63FF33858C74 for ; Sun, 8 Oct 2023 22:58:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63FF33858C74 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696805931; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Bwe+g07rK1rvEaZfgJmhEpYuOpi2FCnXMBwagv5Cbh4=; b=ODbfSDtp2CXr4zde52t9rK4JRfa1ld+jg3cYu9IPakVnIba4wnnzFCZP1WVodUSpHogfwg oXTVFxOVKy1DlJDE2RLIwDVPpZpFgaby63UyzN6Ei1MLk33W8yiFHrkPVCfr8UDCNbUrr0 1TFDsWoxpWmGfVvT6ITYb3wl+iD+rsY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-632-6YNkahNNPimyNazPS77KNw-1; Sun, 08 Oct 2023 18:58:39 -0400 X-MC-Unique: 6YNkahNNPimyNazPS77KNw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4D2F08039C1 for ; Sun, 8 Oct 2023 22:58:39 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.18.119]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23BC949BB9A; Sun, 8 Oct 2023 22:58:39 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] diagnostics: fix ICE on sarif output when source file is unreadable [PR111700] Date: Sun, 8 Oct 2023 18:58:37 -0400 Message-Id: <20231008225837.783598-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779230045840405975 X-GMAIL-MSGID: 1779230045840405975 Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-4474-g94caa6a6b4bd73. gcc/ChangeLog: PR driver/111700 * input.cc (file_cache::add_file): Update leading comment to clarify that it can fail. (file_cache::lookup_or_add_file): Likewise. (file_cache::get_source_file_content): Gracefully handle lookup_or_add_file failing. gcc/testsuite/ChangeLog: PR driver/111700 * c-c++-common/diagnostic-format-sarif-file-pr111700.c: New test. --- gcc/input.cc | 12 ++++++++++-- .../diagnostic-format-sarif-file-pr111700.c | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c diff --git a/gcc/input.cc b/gcc/input.cc index 0b00b8923db..1956f2fcbad 100644 --- a/gcc/input.cc +++ b/gcc/input.cc @@ -443,7 +443,10 @@ file_cache::evicted_cache_tab_entry (unsigned *highest_use_count) accessed by caret diagnostic. This cache is added to an array of cache and can be retrieved by lookup_file_in_cache_tab. This function returns the created cache. Note that only the last - num_file_slots files are cached. */ + num_file_slots files are cached. + + This can return nullptr if the FILE_PATH can't be opened for + reading, or if the content can't be converted to the input_charset. */ file_cache_slot* file_cache::add_file (const char *file_path) @@ -547,7 +550,10 @@ file_cache::~file_cache () /* Lookup the cache used for the content of a given file accessed by caret diagnostic. If no cached file was found, create a new cache for this file, add it to the array of cached file and return - it. */ + it. + + This can return nullptr on a cache miss if FILE_PATH can't be opened for + reading, or if the content can't be converted to the input_charset. */ file_cache_slot* file_cache::lookup_or_add_file (const char *file_path) @@ -1072,6 +1078,8 @@ char_span file_cache::get_source_file_content (const char *file_path) { file_cache_slot *c = lookup_or_add_file (file_path); + if (c == nullptr) + return char_span (nullptr, 0); return c->get_full_file_content (); } diff --git a/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c new file mode 100644 index 00000000000..06605accf6e --- /dev/null +++ b/gcc/testsuite/c-c++-common/diagnostic-format-sarif-file-pr111700.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-fdiagnostics-format=sarif-file" } */ + +# 0 "this-file-does-not-exist.c" +#warning message + +/* Verify that some JSON was written to a file with the expected name. */ +/* { dg-final { verify-sarif-file } } */ + +/* ...and that it at least includes the warning + { dg-final { scan-sarif-file "\"message\": " } } + { dg-final { scan-sarif-file "\"text\": \"#warning message" } } */