From patchwork Fri Oct 13 12:19:51 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: 77659 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 2F5443857BB2 for ; Fri, 13 Oct 2023 12:20:20 +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 BD85F3858D28 for ; Fri, 13 Oct 2023 12:19:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BD85F3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de 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 DD1841FD9E for ; Fri, 13 Oct 2023 12:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1697199579; 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=NnV02HR4D5C5gkRtLBn2YhgnUkllIFmMG/AQjJbHzaA=; b=g2WRiloZlrYncqhHBCBLQWlLHk1Rgk340Ow4pjFE7YoXbneq8Tr4V7DelZGrb+9KfErC6t L97nh+zsN1VxY4G78UzkMZPzsccawsWnLNpSKoAV5qwcuCNU9GnYwcwDCNimGrSeb9/L6L Ia0m9f1qKtvqmrK1wH5XVTXD1BvW5bk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1697199579; 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=NnV02HR4D5C5gkRtLBn2YhgnUkllIFmMG/AQjJbHzaA=; b=IsYAT28Zw1R9sob86UYlvDFehJcP3TOhunHBmnsWuinACXLAWQj2495jki5XJdqd/9CsmC PIi9Rd98TiDFO8Bg== 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 BDDAE13A00 for ; Fri, 13 Oct 2023 12:19:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id gEw9Lds1KWU0bQAAMHmgww (envelope-from ) for ; Fri, 13 Oct 2023 12:19:39 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v2 2/4] [gdb/cli] Factor out try_source_highlight Date: Fri, 13 Oct 2023 14:19:51 +0200 Message-Id: <20231013121953.25917-3-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231013121953.25917-1-tdevries@suse.de> References: <20231013121953.25917-1-tdevries@suse.de> MIME-Version: 1.0 Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: 0.88 X-Spamd-Result: default: False [0.88 / 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]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-3.00)[-1.000]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-1.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(-0.02)[53.82%] 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 chunk 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 | 99 +++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/gdb/source-cache.c b/gdb/source-cache.c index 7ef54411c69..9757721e932 100644 --- a/gdb/source-cache.c +++ b/gdb/source-cache.c @@ -191,6 +191,63 @@ 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); +#if __cplusplus >= 202002L + contents = std::move (output).str(); +#else + contents = output.str (); +#endif + 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,48 +287,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); -#if __cplusplus >= 202002L - contents = std::move (output).str(); -#else - contents = output.str (); -#endif - 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);