From patchwork Mon Jul 27 20:13:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 1337276 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=T49Q6zDH; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BFrbb17Njz9sRN for ; Tue, 28 Jul 2020 06:13:29 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F1963850403; Mon, 27 Jul 2020 20:13:26 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by sourceware.org (Postfix) with ESMTPS id 13D083860C3E for ; Mon, 27 Jul 2020 20:13:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 13D083860C3E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=acm.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nathanmsidwell@gmail.com Received: by mail-qk1-x729.google.com with SMTP id j187so16498369qke.11 for ; Mon, 27 Jul 2020 13:13:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=7vyYE9Rk9HkuAiWHqdPEwNzpUUORcbEcDcNuQXZG+74=; b=T49Q6zDH6+/9eUtouzmqodnVZA4HFtvZfdwg7NEuCW3KZL359zDi+OqjDjtl5qXe4E kzDa4jQ8vDeiurZ6FScEUmAMzgJRm+16Jh5pqoxMHIAfEmftaol97Ze6WE5cu8Ps7k8X ASRZC6WkLMHaRRE4oAtOaYWQ1z8/2hzeQrARGxHrtDRAa7QhO0+NbhoNEKLMUSdrU2N2 qAeCC439Ymt0N6b4+2d8lvkqVleD3kXRB8J+m8BKiF/f2f6lgO7ZieE8OD+VIPSCgn5g fW/SjKv6LmsOeIyqDVPbjoQeH9GMZkvRBtSdjAgbjiIFoS5YGGBJdi+VRa38yHSMoLRj Aetg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:to:from:subject:message-id:date :user-agent:mime-version:content-language; bh=7vyYE9Rk9HkuAiWHqdPEwNzpUUORcbEcDcNuQXZG+74=; b=ch8Wqyo1UAxOTL5vr6d/JsjWoYeDRgRiubnrD+tFufrW7k8eHvwwsYGWHP+EwT1QDV WXucH7Iv2PtUyI+gygO/h7dp4h9Yn9QQvKrRA/8SSKeivsZRzkayLOyRCcY5PX//IWBA Qg98rwhK2qP39di9XMNtsXgBvyQEIDd4WBzWTkiGRdJUoH+AKlHqV/yUBlN5a2D+Ijkk usOy/62jfEBePIDDHRS5Av6JX1FedxzrT6mlPk+ybzWEjzyskQK6O2cFR0w/KncKw9X9 L7oN4oehtSAL03u7W/Ju1umTBCIz1XoZ48jBA/dt4yQElEeiBZ46Q8luLyAnyzTd4d8Q pUnw== X-Gm-Message-State: AOAM530hpBcBVoG57YOOtH8a0ZauBxxS/c3M2DgL36SE8rahuLcalj00 U6RzDl75rY7Y0As0Vfuty/8= X-Google-Smtp-Source: ABdhPJzGmxuR+g0R8AUbhqk2K2hy5BZFmH4WineSW19jTFTnFrI+tKRQsvpGdpSo3idUqXoYEcIdQA== X-Received: by 2002:a37:44b:: with SMTP id 72mr23123728qke.494.1595880803238; Mon, 27 Jul 2020 13:13:23 -0700 (PDT) Received: from ?IPv6:2620:10d:c0a8:1102:e915:818c:4abe:54fb? ([2620:10d:c091:480::1:73c2]) by smtp.googlemail.com with ESMTPSA id f53sm16016835qta.84.2020.07.27.13.13.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 27 Jul 2020 13:13:22 -0700 (PDT) To: GCC Patches From: Nathan Sidwell Subject: c-family: Use strcmp to compare location file names Message-ID: <9c569d77-7467-6b25-cd77-5e7648be0a03@acm.org> Date: Mon, 27 Jul 2020 16:13:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_LINEPADDING, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" The logic to figure out where a missing #include should be inserted uses pointer equality to check filenames -- the routine even says so. But cpplib makes no such guarantee. It happens to be true for input that it preprocesses[* see line zero below], but is not true for source that has already been preprocessed -- all those '# ...' line directives produce disctinct filename strings. That renders using -fdirectives-only as a prescanning stage (as I understand some people do), broken. This patch changes to string comparisons, and explicitly rejects any line-zero location map that occurs at the beginning of a file. The very first map of a file has a different string to the remaining maps, and we never tripped on that because of the pointer comparison. The second testcase deploys -save-temps to cause an intermediate preprocessed output that is read back. gcc/c-family/ * c-common.c (try_to_locate_new_include_insertion_point): Use strcmp, not pointer equality. gcc/testsuite/ * g++.dg/lookup/missing-std-include-10.h: New. * g++.dg/lookup/missing-std-include-10.C: New. * g++.dg/lookup/missing-std-include-11.C: New. pushing ... diff --git c/gcc/c-family/c-common.c w/gcc/c-family/c-common.c index 51ecde69f2d..98b80d56cae 100644 --- c/gcc/c-family/c-common.c +++ w/gcc/c-family/c-common.c @@ -8764,8 +8764,7 @@ c_family_tests (void) #endif /* #if CHECKING_P */ /* Attempt to locate a suitable location within FILE for a - #include directive to be inserted before. FILE should - be a string from libcpp (pointer equality is used). + #include directive to be inserted before. LOC is the location of the relevant diagnostic. Attempt to return the location within FILE immediately @@ -8800,13 +8799,17 @@ try_to_locate_new_include_insertion_point (const char *file, location_t loc) if (const line_map_ordinary *from = linemap_included_from_linemap (line_table, ord_map)) - if (from->to_file == file) + /* We cannot use pointer equality, because with preprocessed + input all filename strings are unique. */ + if (0 == strcmp (from->to_file, file)) { last_include_ord_map = from; last_ord_map_after_include = NULL; } - if (ord_map->to_file == file) + /* Likewise, use strcmp, and reject any line-zero introductory + map. */ + if (ord_map->to_line && 0 == strcmp (ord_map->to_file, file)) { if (!first_ord_map_in_file) first_ord_map_in_file = ord_map; diff --git c/gcc/testsuite/g++.dg/lookup/missing-std-include-10.C w/gcc/testsuite/g++.dg/lookup/missing-std-include-10.C new file mode 100644 index 00000000000..9dfa78fb60e --- /dev/null +++ w/gcc/testsuite/g++.dg/lookup/missing-std-include-10.C @@ -0,0 +1,43 @@ +// { dg-do compile } +// { dg-additional-options -fdiagnostics-show-caret } +// comment + + + + + + +// Intentional blank lines + + + + + + + + +#include "missing-std-include-10.h" +// HERE + + + + + + +// Intentional blank lines + + + + + + + + + +int main () +{ + return strcmp ("", ""); +} +// { dg-additional-files "missing-std-include-10.h" } +// { dg-regexp {[^\n]*: error: 'strcmp' was not declared in this scope\n *return strcmp [^\n]*;\n *\^~*\n} } +// { dg-regexp {[^\n]* note: 'strcmp' is defined in header[^\n]*\n #include "missing-std-include-10.h"\n\+#include \n // HERE\n} } diff --git c/gcc/testsuite/g++.dg/lookup/missing-std-include-10.h w/gcc/testsuite/g++.dg/lookup/missing-std-include-10.h new file mode 100644 index 00000000000..40a8c178f10 --- /dev/null +++ w/gcc/testsuite/g++.dg/lookup/missing-std-include-10.h @@ -0,0 +1 @@ +/* empty */ diff --git c/gcc/testsuite/g++.dg/lookup/missing-std-include-11.C w/gcc/testsuite/g++.dg/lookup/missing-std-include-11.C new file mode 100644 index 00000000000..ec2c494c557 --- /dev/null +++ w/gcc/testsuite/g++.dg/lookup/missing-std-include-11.C @@ -0,0 +1,43 @@ +// { dg-do compile } +// { dg-additional-options {-fdiagnostics-show-caret -save-temps} } +// comment save-temps causes us to compile preprocessed output + + + + + + +// Intentional blank lines + + + + + + + + +#include "missing-std-include-10.h" +// HERE + + + + + + +// Intentional blank lines + + + + + + + + + +int main () +{ + return strcmp ("", ""); +} +// { dg-additional-files "missing-std-include-10.h" } +// { dg-regexp {[^\n]*: error: 'strcmp' was not declared in this scope\n *return strcmp [^\n]*;\n *\^~*\n} } +// { dg-regexp {[^\n]* note: 'strcmp' is defined in header[^\n]*\n #include "missing-std-include-10.h"\n\+#include \n // HERE\n} }