From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E39B738768A4; Fri, 24 Mar 2023 15:41:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E39B738768A4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679672485; bh=rTmZEBDW91nUZRy4Jd0SMXvuibdRyLZR3Wni1WLcYJA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=aQhgMiZpmtQrTIHvliPuTNeALB0xi2CGgjq8G1yZ4dRpqrnXKeNVI41XWiaKIcPAQ tUZAqIYPrfSAq/sg9bb78maMe9oQim0EGJFWt08OyaC9cOyfP5N7Wftb7lxAnAxhpX HV7aMvuE1JlWAY1SWxqSdZzz57/YWgxYYvijAIKg= From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug other/109163] SARIF (and other JSON) output files are non-deterministic Date: Fri, 24 Mar 2023 15:41:25 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: other X-Bugzilla-Version: 13.0 X-Bugzilla-Keywords: patch X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: WAITING X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109163 --- Comment #4 from CVS Commits --- The master branch has been updated by David Malcolm : https://gcc.gnu.org/g:7f1e15f743357e037d7c4f6f6000863c26f3dfc3 commit r13-6851-g7f1e15f743357e037d7c4f6f6000863c26f3dfc3 Author: David Malcolm Date: Fri Mar 24 11:38:14 2023 -0400 json: preserve key-insertion order [PR109163] PR other/109163 notes that when we write out JSON files, we traverse the keys within each object via hash_map iteration, and thus the ordering is non-deterministic - it can arbitrarily vary from run to run and from different machines, making it harder for users to compare results and determine if anything has "really" changed. I'm running into this issue with SARIF output, but there are several places where we're currently emitting JSON: * -fsave-optimization-record emits SRCFILE.opt-record.json.gz "This option is experimental and the format of the data within the compressed JSON file is subject to change."; see optinfo-emit-json.{h,cc}, dumpfile.cc, etc * -fdiagnostics-format=3D with the various "sarif" and "json" options * -fdump-analyzer-json is a developer option in the analyzer * gcov has: "-j, --json-format: Output JSON intermediate format into .gcov.json.gz file" This patch adds an auto_vec to class json::object to preserve key-insertion order, and use it when writing out objects. Potentially this slightly slows down JSON output, but I believe that this isn't normally a bottleneck, and that the benefits to the user of deterministic output are worth it. I had first attempted to use ordered_hash_map.h for this, but ran into impenetrable template errors, so this patch uses a simpler approach of just adding an auto_vec to json::object. Testing showed a failure of diagnostic-format-json-5.c, which was using a convoluted set of regexps to consume the output; I believe that this was brittle, and was intermittently failing for some of the random orderings of output. I rewrote these regexps to work with the expected output order. The other such tests seem to pass with the now-deterministic orderings. gcc/ChangeLog: PR other/109163 * json.cc: Update comments to indicate that we now preserve insertion order of keys within objects. (object::print): Traverse keys in insertion order. (object::set): Preserve insertion order of keys. (selftest::test_writing_objects): Add an additional key to veri= fy that we preserve insertion order. * json.h (object::m_keys): New field. gcc/testsuite/ChangeLog: PR other/109163 * c-c++-common/diagnostic-format-json-1.c: Update comment. * c-c++-common/diagnostic-format-json-2.c: Likewise. * c-c++-common/diagnostic-format-json-3.c: Likewise. * c-c++-common/diagnostic-format-json-4.c: Likewise. * c-c++-common/diagnostic-format-json-5.c: Rewrite regexps. * c-c++-common/diagnostic-format-json-stderr-1.c: Update commen= t. Signed-off-by: David Malcolm =