From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 7C10738555AD for ; Wed, 5 Apr 2023 14:05:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C10738555AD Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x335.google.com with SMTP id m6-20020a05600c3b0600b003ee6e324b19so22072254wms.1 for ; Wed, 05 Apr 2023 07:05:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703535; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=54dS6FrJwYhk+06LJqCTGLmIFZu5DQOrMSHvcyd5Q/c=; b=PcvdxtYQ8HfaQjhzwh/k53gNTWnOhqfXLLil5lWa/BS6uRT26Co3bkNhZY/TsQX3t4 NXLqGQMcDePb816rsFFNjL/8laE69SYmXPiK/KeggTaRMqavy7rdBma/onHvgdFaVzYh ilSM3r4XZGnX4/NWf35c0VXfmWHfxtIQoqY0fm8LP97124fbXJ+YFd7q7KqJoCEh9rWn 0GWmgOYEn80Vfnt3i2ulkZK5hcTaUlZ8D8uLCd/ckvFtvyrSe9vFdBXc5qIF9MtcSpT4 mDGczLXQztFKi9jt1Hf6AYut3d67/scrD+j7VRCueMc5yFzo0RqST+0Sqm1p851SVtf6 uFvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703535; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=54dS6FrJwYhk+06LJqCTGLmIFZu5DQOrMSHvcyd5Q/c=; b=muRqMC7nlZy8FrUC2joanHXoeip/6+8qd5uWlzHSxNTxu9ZbNCg850YKawZnipveC7 cNSWmsNwXtF/cks77uLFuP2Bk3d3gB8DJ0CO+EvNnjTArmxEBLEKwwGKTPqHAYR2JROU 8Q9ZX8rnRv/TuA3t8YUqdEU8OBGLJzNZizSM+34YDd02gB5wT547b++QwZa66mEEkl/+ QqUJ6l1jYHOlzKB1s9jXqjIxkY3dtKP64iIIZjC48L0Y3vME9tmudwfvNUyjfvToi9ij Y21MnTKwCQhEjgOh9Ktw7F9YvA7WiR6HSpPeFQlMTzY9mqLpVabqHwibJwUzbHFG2kZ6 nEYQ== X-Gm-Message-State: AAQBX9e40dTY1kWC2YhboEC4p+MpdjDTH/puGdWi1cKrl3KodqCFYHU5 ItRXg431FleHGp6+fj5QEo6R X-Google-Smtp-Source: AKy350YSOfiN9uouU3QkHuH/+4exKh5Xl5kWceg/EE+swq93Sbzje4bay0C8QBGIbKwF8ykQmbFyPA== X-Received: by 2002:a05:600c:25a:b0:3ed:237f:3da with SMTP id 26-20020a05600c025a00b003ed237f03damr5204318wmj.22.1680703535210; Wed, 05 Apr 2023 07:05:35 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.05.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:34 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, mxlol233 Subject: [committed 10/88] gccrs: diagnostics: Add underline for tokens in diagnostics. Date: Wed, 5 Apr 2023 16:02:54 +0200 Message-Id: <20230405140411.3016563-11-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.6 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 List-Id: From: mxlol233 Currently, the diagnostics only point to the corresponding token's start position by carets, and lack of underlines for full token. This commit add support for such underlines in diagnostics by encoding range information in location_t. gcc/rust/ChangeLog: * lex/rust-lex.cc (Lexer::build_token): Make location enclose entire token. (Lexer::parse_byte_char): Likewise. (Lexer::parse_byte_string): Likewise. (Lexer::parse_raw_byte_string): Likewise. (Lexer::parse_raw_identifier): Likewise. (Lexer::parse_string): Likewise. (Lexer::parse_identifier_or_keyword): Likewise. (Lexer::parse_raw_string): Likewise. (Lexer::parse_non_decimal_int_literal): Likewise. (Lexer::parse_decimal_int_or_float): Likewise. (Lexer::parse_char_or_lifetime): Likewise. gcc/testsuite/ChangeLog: * rust/compile/diagnostic_underline.rs: New test. Signed-off-by: Xiao Ma --- gcc/rust/lex/rust-lex.cc | 57 +++++++++++++++++++ .../rust/compile/diagnostic_underline.rs | 15 +++++ 2 files changed, 72 insertions(+) create mode 100644 gcc/testsuite/rust/compile/diagnostic_underline.rs diff --git a/gcc/rust/lex/rust-lex.cc b/gcc/rust/lex/rust-lex.cc index ac7ced540e6..9967cecb2e2 100644 --- a/gcc/rust/lex/rust-lex.cc +++ b/gcc/rust/lex/rust-lex.cc @@ -447,6 +447,7 @@ Lexer::build_token () // match arm arrow skip_input (); current_column += 2; + loc += 1; return Token::make (MATCH_ARROW, loc); } @@ -455,6 +456,7 @@ Lexer::build_token () // equality operator skip_input (); current_column += 2; + loc += 1; return Token::make (EQUAL_EQUAL, loc); } @@ -473,6 +475,7 @@ Lexer::build_token () // return type specifier skip_input (); current_column += 2; + loc += 1; return Token::make (RETURN_TYPE, loc); } @@ -481,6 +484,7 @@ Lexer::build_token () // minus-assign skip_input (); current_column += 2; + loc += 1; return Token::make (MINUS_EQ, loc); } @@ -496,6 +500,7 @@ Lexer::build_token () // add-assign skip_input (); current_column += 2; + loc += 1; return Token::make (PLUS_EQ, loc); } @@ -517,6 +522,7 @@ Lexer::build_token () // multiplication-assign skip_input (); current_column += 2; + loc += 1; return Token::make (ASTERISK_EQ, loc); } @@ -535,6 +541,7 @@ Lexer::build_token () // division-assign skip_input (); current_column += 2; + loc += 1; return Token::make (DIV_EQ, loc); } @@ -602,6 +609,8 @@ Lexer::build_token () start_line (current_line, max_column_hint); str.shrink_to_fit (); + + loc += str.size () - 1; if (is_inner) return Token::make_inner_doc_comment (loc, std::move (str)); else @@ -756,6 +765,8 @@ Lexer::build_token () } str.shrink_to_fit (); + + loc += str.size () - 1; if (is_inner) return Token::make_inner_doc_comment (loc, std::move (str)); else @@ -773,6 +784,7 @@ Lexer::build_token () // modulo-assign skip_input (); current_column += 2; + loc += 1; return Token::make (PERCENT_EQ, loc); } @@ -788,6 +800,7 @@ Lexer::build_token () // xor-assign? skip_input (); current_column += 2; + loc += 1; return Token::make (CARET_EQ, loc); } @@ -805,6 +818,7 @@ Lexer::build_token () // left-shift assign skip_input (1); current_column += 3; + loc += 2; return Token::make (LEFT_SHIFT_EQ, loc); } @@ -813,6 +827,7 @@ Lexer::build_token () // left-shift skip_input (); current_column += 2; + loc += 1; return Token::make (LEFT_SHIFT, loc); } @@ -822,6 +837,7 @@ Lexer::build_token () // smaller than or equal to skip_input (); current_column += 2; + loc += 1; return Token::make (LESS_OR_EQUAL, loc); } @@ -840,6 +856,7 @@ Lexer::build_token () // right-shift-assign skip_input (1); current_column += 3; + loc += 2; return Token::make (RIGHT_SHIFT_EQ, loc); } @@ -848,6 +865,7 @@ Lexer::build_token () // right-shift skip_input (); current_column += 2; + loc += 1; return Token::make (RIGHT_SHIFT, loc); } @@ -857,6 +875,7 @@ Lexer::build_token () // larger than or equal to skip_input (); current_column += 2; + loc += 1; return Token::make (GREATER_OR_EQUAL, loc); } @@ -872,6 +891,7 @@ Lexer::build_token () // scope resolution :: skip_input (); current_column += 2; + loc += 1; return Token::make (SCOPE_RESOLUTION, loc); } @@ -888,6 +908,7 @@ Lexer::build_token () // not equal boolean operator skip_input (); current_column += 2; + loc += 1; return Token::make (NOT_EQUAL, loc); } @@ -937,6 +958,7 @@ Lexer::build_token () // bitwise or-assign? skip_input (); current_column += 2; + loc += 1; return Token::make (PIPE_EQ, loc); } @@ -945,6 +967,7 @@ Lexer::build_token () // logical or skip_input (); current_column += 2; + loc += 1; return Token::make (OR, loc); } @@ -961,6 +984,7 @@ Lexer::build_token () // bitwise and-assign? skip_input (); current_column += 2; + loc += 1; return Token::make (AMP_EQ, loc); } @@ -969,6 +993,7 @@ Lexer::build_token () // logical and skip_input (); current_column += 2; + loc += 1; return Token::make (LOGICAL_AND, loc); } @@ -987,6 +1012,7 @@ Lexer::build_token () // ellipsis skip_input (1); current_column += 3; + loc += 2; return Token::make (ELLIPSIS, loc); } @@ -995,6 +1021,7 @@ Lexer::build_token () // ..= skip_input (1); current_column += 3; + loc += 2; return Token::make (DOT_DOT_EQ, loc); } @@ -1003,6 +1030,7 @@ Lexer::build_token () // .. skip_input (); current_column += 2; + loc += 1; return Token::make (DOT_DOT, loc); } @@ -1717,6 +1745,8 @@ Lexer::parse_byte_char (Location loc) current_column += length; + loc += length - 1; + return Token::make_byte_char (loc, byte_char); } @@ -1781,6 +1811,7 @@ Lexer::parse_byte_string (Location loc) } str.shrink_to_fit (); + loc += str.size () - 1; return Token::make_byte_string (loc, std::move (str)); } @@ -1861,6 +1892,8 @@ Lexer::parse_raw_byte_string (Location loc) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_byte_string (loc, std::move (str)); @@ -1912,6 +1945,7 @@ Lexer::parse_raw_identifier (Location loc) else { str.shrink_to_fit (); + loc += length - 1; return Token::make_identifier (loc, std::move (str)); } @@ -2009,6 +2043,8 @@ Lexer::parse_string (Location loc) } str.shrink_to_fit (); + loc += length - 1; + return Token::make_string (loc, std::move (str)); } @@ -2043,6 +2079,8 @@ Lexer::parse_identifier_or_keyword (Location loc) str.shrink_to_fit (); + loc += length - 1; + TokenId keyword = classify_keyword (str); if (keyword == IDENTIFIER) return Token::make_identifier (loc, std::move (str)); @@ -2120,6 +2158,8 @@ Lexer::parse_raw_string (Location loc, int initial_hash_count) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_string (loc, std::move (str)); @@ -2183,6 +2223,9 @@ Lexer::parse_non_decimal_int_literal (Location loc, IsDigitFunc is_digit_func, : ""))); return nullptr; } + + loc += length - 1; + return Token::make_int (loc, std::move (existent_str), type_hint); } @@ -2275,6 +2318,8 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_float (loc, std::move (str), type_hint); } @@ -2295,6 +2340,8 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_float (loc, std::move (str), CORETYPE_UNKNOWN); } @@ -2324,6 +2371,8 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_float (loc, std::move (str), type_hint); } @@ -2345,6 +2394,8 @@ Lexer::parse_decimal_int_or_float (Location loc) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_int (loc, std::move (str), type_hint); } @@ -2382,6 +2433,8 @@ Lexer::parse_char_or_lifetime (Location loc) current_column += length; + loc += length - 1; + return Token::make_char (loc, current_char32); } else @@ -2399,6 +2452,8 @@ Lexer::parse_char_or_lifetime (Location loc) // TODO fix due to different widths of utf-8 chars? current_column += 3; + loc += 2; + return Token::make_char (loc, current_char32); } else if (ISDIGIT (current_char32.value) || ISALPHA (current_char32.value) @@ -2421,6 +2476,8 @@ Lexer::parse_char_or_lifetime (Location loc) current_column += length; + loc += length - 1; + str.shrink_to_fit (); return Token::make_lifetime (loc, std::move (str)); } diff --git a/gcc/testsuite/rust/compile/diagnostic_underline.rs b/gcc/testsuite/rust/compile/diagnostic_underline.rs new file mode 100644 index 00000000000..fcbf468e1c8 --- /dev/null +++ b/gcc/testsuite/rust/compile/diagnostic_underline.rs @@ -0,0 +1,15 @@ +// { dg-additional-options "-quiet" } + +/* { dg-options "-fdiagnostics-show-caret" } */ + + +fn barbarbar() {} + +const fn foo() { + barbarbar();// { dg-error "only functions marked as 'const' are allowed to be called from constant contexts" } +/* { dg-begin-multiline-output "" } + barbarbar();// + ^~~~~~~~~ +{ dg-end-multiline-output "" } */ +} + -- 2.40.0