From patchwork Mon Oct 16 09:17:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 77870 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 4787538560AA for ; Mon, 16 Oct 2023 09:17:47 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 60C62385842C for ; Mon, 16 Oct 2023 09:17:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 60C62385842C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 60C62385842C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.220.29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; cv=none; b=FxT2KCr26GTucF1iEwKLTVDYW4WNoT6AEOZrrXSKFaPBbFBR5CeHdbePS0hz/yArHtQ/Gp251clpnGLgV/Ow5T3LT4vuTtKZRNYDC+55KiIKrWUbvfqfDIJ5C5hfhgjq6Oj1/fBUXaq8OjftNitJpRVYAYdj4EmFKQ94P5xnF+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697447850; c=relaxed/simple; bh=sbM8d21bgC8kiaDoGuyqqK1nw9fPYR4nU1fBPSb6uFI=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-Id: MIME-Version; b=e6A8Oksyyr2bVTJRMhMMgpp6ovkxCMXehOtSf/H9GgOLB8utiNhB6UKzOM/nMKFmSkFBD4S2mnrY94BJUTqO7YU6o02Gse5Lavy8msCBQFzZA2jersGksjiOLUnaFIx5lTRM2AaIVSURklurQ/k4ljTpAqvWef4NswWn/OvLmnI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 596221FEB7 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JGb/vVp36UHzsoogsr1awMSvJS4+/6M8VuhFWeQM+q0=; b=cWR9k75u0qBe8HItUZZIO6CwZRmXHmFduJPKFl1t/qzfCpW6cMm4Ci+0gi76EUwWSjEAXD VeClaLXH1DCKeLIm7nesFl0L4ZZCSNpjxwq+h7FhX6Y3dQZc8P9BYmVn49kvnnqYpFXyto xUw2flsnrGxBxKqxWzS2lQLMFcxy3K8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697447847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JGb/vVp36UHzsoogsr1awMSvJS4+/6M8VuhFWeQM+q0=; b=UelNxbOiGZTd7h4ckM82CxlRS95XXst9zeC4xJ2+ZZxp17Bow6vRFHNLQCkhF5e7pp49qa UaeN7L1ELp8bANDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4266E13A20 for ; Mon, 16 Oct 2023 09:17:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 2Mk6D6f/LGUXLQAAMHmgww (envelope-from ) for ; Mon, 16 Oct 2023 09:17:27 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v3 2/4] [gdb/cli] Factor out try_source_highlight Date: Mon, 16 Oct 2023 11:17:46 +0200 Message-Id: <20231016091748.26247-3-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231016091748.26247-1-tdevries@suse.de> References: <20231016091748.26247-1-tdevries@suse.de> MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Score: 4.96 X-Spamd-Result: default: False [4.96 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_MISSING_CHARSET(2.50)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.97)[-0.974]; NEURAL_SPAM_LONG(3.00)[1.000]; MID_CONTAINS_FROM(1.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-1.96)[94.84%] X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 Function source_cache::ensure contains some code using the GNU source-highlight library. The code is a sizable part of the function, and contains conditional compilation in a slightly convoluted way: ... if (!already_styled) #endif /* HAVE_SOURCE_HIGHLIGHT */ { ... Fix this by factoring out the code into new function try_source_highlight, such that: - source_cache::ensure is easier to read, and - the conditional compilation is at the level of the function body. Tested on x86_64-linux. --- gdb/source-cache.c | 91 ++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index ae02d2516d9..191d0043e66 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -191,6 +191,59 @@ get_language_name (enum language lang) #endif /* HAVE_SOURCE_HIGHLIGHT */ +/* Try to highlight CONTENTS from file FULLNAME in language LANG using + the GNU source-higlight library. Return true if highlighting + succeeded. */ + +static bool +try_source_highlight (std::string &contents ATTRIBUTE_UNUSED, + enum language lang ATTRIBUTE_UNUSED, + const std::string &fullname ATTRIBUTE_UNUSED) +{ +#ifdef HAVE_SOURCE_HIGHLIGHT + if (!use_gnu_source_highlight) + return false; + + const char *lang_name = get_language_name (lang); + if (lang_name == nullptr) + return false; + + /* The global source highlight object, or null if one was + never constructed. This is stored here rather than in + the class so that we don't need to include anything or do + conditional compilation in source-cache.h. */ + static srchilite::SourceHighlight *highlighter; + + bool styled = false; + try + { + if (highlighter == nullptr) + { + highlighter = new srchilite::SourceHighlight ("esc.outlang"); + highlighter->setStyleFile ("esc.style"); + } + + std::istringstream input (contents); + std::ostringstream output; + highlighter->highlight (input, output, lang_name, fullname); + contents = std::move (output).str(); + styled = true; + } + catch (...) + { + /* Source Highlight will throw an exception if + highlighting fails. One possible reason it can fail + is if the language is unknown -- which matters to gdb + because Rust support wasn't added until after 3.1.8. + Ignore exceptions here. */ + } + + return styled; +#else + return false; +#endif /* HAVE_SOURCE_HIGHLIGHT */ +} + /* See source-cache.h. */ bool @@ -230,44 +283,10 @@ source_cache::ensure (struct symtab *s) if (source_styling && gdb_stdout->can_emit_style_escape ()) { -#ifdef HAVE_SOURCE_HIGHLIGHT - bool already_styled = false; - const char *lang_name = get_language_name (s->language ()); - if (lang_name != nullptr && use_gnu_source_highlight) - { - /* The global source highlight object, or null if one was - never constructed. This is stored here rather than in - the class so that we don't need to include anything or do - conditional compilation in source-cache.h. */ - static srchilite::SourceHighlight *highlighter; - - try - { - if (highlighter == nullptr) - { - highlighter = new srchilite::SourceHighlight ("esc.outlang"); - highlighter->setStyleFile ("esc.style"); - } - - std::istringstream input (contents); - std::ostringstream output; - highlighter->highlight (input, output, lang_name, fullname); - contents = std::move (output).str (); - already_styled = true; - } - catch (...) - { - /* Source Highlight will throw an exception if - highlighting fails. One possible reason it can fail - is if the language is unknown -- which matters to gdb - because Rust support wasn't added until after 3.1.8. - Ignore exceptions here and fall back to - un-highlighted text. */ - } - } + bool already_styled + = try_source_highlight (contents, s->language (), fullname); if (!already_styled) -#endif /* HAVE_SOURCE_HIGHLIGHT */ { gdb::optional ext_contents; ext_contents = ext_lang_colorize (fullname, contents);