From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E002D3858D37; Wed, 21 Feb 2024 13:58:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E002D3858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708523929; bh=2ZqRIBfCK4coDQBzcVxNkvXViIaXw7o1LTE9XkrbHks=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Bsk+d684beAJ252te2TMVI+XsOUjXeRs+DkOlwFRyBb/SlfX9cfb9GRwMZRmCyFxk KZnnehAYKvUG6QAB62WqHcB6dRMDG19+J5YxgBE9K9VSzs36Re3tjBpxBjpRwcwqy4 5mYu36ha7CzYCTtNJ/KJsGzZxSPBVO06HmuuSboM= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug preprocessor/114007] gcc chokes on __has_cpp_attribute(clang::unsafe_buffer_usage) Date: Wed, 21 Feb 2024 13:58:47 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: preprocessor X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 14.0 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc 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=3D114007 Jakub Jelinek changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |rsandifo at gcc dot gnu.org --- Comment #13 from Jakub Jelinek --- Ah, the thing is that while in -std=3Dgnu* modes or -std=3Dc23 the preproce= ssor recognizes CPP_SCOPE as one token, in -std=3Dc{89,99,11,17} modes it doesn'= t, :: are 2 CPP_COLONs. So, we could either: --- gcc/c-family/c-lex.cc.jj 2024-01-03 12:07:02.171734141 +0100 +++ gcc/c-family/c-lex.cc 2024-02-21 14:30:37.247945782 +0100 @@ -357,7 +357,24 @@ c_common_has_attribute (cpp_reader *pfil do nxt_token =3D cpp_peek_token (pfile, idx++); while (nxt_token->type =3D=3D CPP_PADDING); - if (nxt_token->type =3D=3D CPP_SCOPE) + if (!c_dialect_cxx () + && flag_iso + && !flag_isoc23 + && nxt_token->type =3D=3D CPP_COLON) + { + do + nxt_token =3D cpp_peek_token (pfile, idx++); + while (nxt_token->type =3D=3D CPP_PADDING); + if (nxt_token->type =3D=3D CPP_COLON) + { + /* __has_attribute (vendor::attr) in -std=3Dc17 etc. modes. + :: isn't CPP_SCOPE in there and [[vendor::attr]] will + not work, only [[__extension__ vendor::attr]]. */ + have_scope =3D true; + get_token_no_padding (pfile); // Eat first colon. + } + } + if (nxt_token->type =3D=3D CPP_SCOPE || have_scope) { have_scope =3D true; get_token_no_padding (pfile); // Eat scope. but then on testcase like: #if __has_c_attribute (gnu::unused) [[gnu::unused]] #endif int i; #if __has_cpp_attribute (gnu::unused) [[gnu::unused]] #endif int j; fails to compile with e.g -std=3Dc11: pr114007.c:2:1: warning: =E2=80=98gnu=E2=80=99 attribute ignored [-Wattribu= tes] 2 | [[gnu::unused]] | ^ pr114007.c:2:6: error: expected =E2=80=98]=E2=80=99 before =E2=80=98:=E2=80= =99 token 2 | [[gnu::unused]] | ^ | ] pr114007.c:6:1: warning: =E2=80=98gnu=E2=80=99 attribute ignored [-Wattribu= tes] 6 | [[gnu::unused]] | ^ pr114007.c:6:6: error: expected =E2=80=98]=E2=80=99 before =E2=80=98:=E2=80= =99 token 6 | [[gnu::unused]] | ^ | ] or we could force always returning 0 from __has_attribute/__has_cpp_attribu= te in that case, like: --- gcc/c-family/c-lex.cc.jj 2024-01-03 12:07:02.171734141 +0100 +++ gcc/c-family/c-lex.cc 2024-02-21 14:41:33.768992572 +0100 @@ -357,7 +357,33 @@ c_common_has_attribute (cpp_reader *pfil do nxt_token =3D cpp_peek_token (pfile, idx++); while (nxt_token->type =3D=3D CPP_PADDING); - if (nxt_token->type =3D=3D CPP_SCOPE) + if (!c_dialect_cxx () + && flag_iso + && !flag_isoc23 + && nxt_token->type =3D=3D CPP_COLON) + { + do + nxt_token =3D cpp_peek_token (pfile, idx++); + while (nxt_token->type =3D=3D CPP_PADDING); + if (nxt_token->type =3D=3D CPP_COLON) + /* __has_attribute (vendor::attr) in -std=3Dc17 etc. modes. + :: isn't CPP_SCOPE in there but 2 CPP_COLON tokens. */ + have_scope =3D true; + } + if (have_scope) + { + /* [[vendor::attr]] will not work, only + [[__extension__ vendor::attr]] will. Better always return 0 + for scoped attributes. */ + get_token_no_padding (pfile); // Eat first colon. + get_token_no_padding (pfile); // Eat second colon. + nxt_token =3D get_token_no_padding (pfile); + if (nxt_token->type !=3D CPP_NAME) + cpp_error (pfile, CPP_DL_ERROR, + "attribute identifier required after scope"); + attr_name =3D NULL_TREE; + } + else if (nxt_token->type =3D=3D CPP_SCOPE) { have_scope =3D true; get_token_no_padding (pfile); // Eat scope. The drawback of the second patch is that then users in -std=3Dc{89,99,11,17} modes don't have a way to query whether a certain scoped attribute is suppo= rted in the preprocessor if they are aware that they need to use [[__extension__ vendor::attr]] rather then [[vendor::attr]]. On the other side, e.g. in -std=3Dgnu11 -pedantic-errors compilation we give 1 for __has_c_attribute (gnu::unused), but it is still rejected, ju= st with -std=3Dc11 it is rejected even without -Wpedantic. Maybe instead of loose_scope_p we should be using flag_iso && !flag_isoc23 = and accept [[vendor: :attr]] in the -std=3Dc{89,99,11,17} modes too (with pedwa= rn for the [[]] use), and on the other side reject [[__extension__ vendor: :attr]]= in -std=3Dc23 or -std=3Dgnu{89,99,11,17} modes, so that people don't feel using 2 colons = rather than a scope is correct. And then perhaps go with the first patch rather t= han second. Joseph/Richard, your thoughts on this?=