From patchwork Wed Feb 14 16:28:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 873425 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-473282-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="jq/t54c1"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zhPwt5K1Cz9t3M for ; Thu, 15 Feb 2018 03:28:45 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=ICLMcuL7apM59pmGKF0uWgDZyqe//hpoH8/WjSntDAd6/+b/On G8/ncGtwGsDs8nsX1fiQPszVGBXkpa9J9SoCaJkAZj+ZHZ+ZTOso3n/C4oYMsyLo cvUo5KxZdbg/HP9j8iv+K+XotPUrVYHFV1hTt/zAxfISbUEhIUnEiilUM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=2HkMrXp/cl7DktUc7Aj+fzHtwnc=; b=jq/t54c1nNoNCqG2eM1E 7PwSZ9uA9Fve/USmtPCslA086I8B3otSs5KOGAf1L2YtCm4egcbcpSv6nkKlIwl7 HHUO9yzwnmEqMHx1T9pbkuaCNpAXMVfy4t7/nHW2BYnadwbas2IA59kDnfnv21qq kZzQv/tky4juVcJ2QQW9Wn0= Received: (qmail 109348 invoked by alias); 14 Feb 2018 16:28:38 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 109336 invoked by uid 89); 14 Feb 2018 16:28:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=attractive, synthesis, Hx-languages-length:4176 X-HELO: mail-yw0-f169.google.com Received: from mail-yw0-f169.google.com (HELO mail-yw0-f169.google.com) (209.85.161.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 14 Feb 2018 16:28:35 +0000 Received: by mail-yw0-f169.google.com with SMTP id e65so6577888ywh.9 for ; Wed, 14 Feb 2018 08:28:35 -0800 (PST) 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=IUDZK2rVagi5AbcnXujZfreoXrBiRXNerT2RA5asij4=; b=eLfF6b/3wJ0rW/yLdjAvn5d2k1dDkr1dCjN+ehxgYCdPLIYVPQ75bxBg3sjN4DLawv IgYRrYjkoie+QME+qZRE4zq+RlX4GdMjAcjHUS0sCa/eCAWP5Eb7KRBJ8OIRh2lCa3Ki IZdZpLjxnTDInXTMl9FesafEE++iUiDYFvgUoFfX+l22b8O/sbITCszv79eOlcsGpv0n Ub+fL/DPrAr11kO09W2hsfpO3elIKaZSAGHFNpgD34oL7EXZLeTHPLsqvxJMeiWoCLTJ wv2SlvG4YeMJL+LfaI5JwfuQybFbjDfN4dcsVUr7dCLGDpoEOhiBHrYuhCx1F1P2gSoG aa9w== X-Gm-Message-State: APf1xPDzncjxdBD5W0iEYtYg7wS3X7bidk9UvevaoUqhkEqplKz1qYfT SYm6yRAwBWv+1O3+YBkzGnk= X-Google-Smtp-Source: AH8x2268sejlH0MMe+CFTAJ/7mqDRJuHcWTcCdM3jWrciToOvUts698F2QVWOXAB4cW9n5c33Nizqw== X-Received: by 10.129.42.6 with SMTP id q6mr3640778ywq.155.1518625713734; Wed, 14 Feb 2018 08:28:33 -0800 (PST) Received: from ?IPv6:2620:10d:c0a3:20fb:7500:e7fb:4a6f:2254? ([2620:10d:c091:200::9317]) by smtp.googlemail.com with ESMTPSA id n2sm1810778ywl.41.2018.02.14.08.28.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Feb 2018 08:28:32 -0800 (PST) To: GCC Patches From: Nathan Sidwell Subject: [C++ PATCH]: instantiation via vtable marking Message-ID: <8cda4082-5b50-289b-eba6-7ba59de39fb9@acm.org> Date: Wed, 14 Feb 2018 11:28:31 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 We had encountered a bogus warning about class visibility. Namely a lambda had captured a variable whose type was in the anonymous namespace. The problem was that in_main_input_context was returning false (i.e. we're in a header file), as that was the location of the outermost template instantiation (yup, the lambda came out of a template instantiation). The problem was the location was wrong -- it was pointing to some much earlier non-template class definition. (funnily enough, the class was to do with Futures, messing with my head about reaching into the future of the compilation). The problem stemmed from the c_parse_final_cleanups. That consists of a do-until-no-more loop that does a bunch of things. Early in the loop it maybe_emits_vtables and later in the loop it emits inline function that need a body. In this case, an earlier non-template class needed its virtual deleting dtor synthesized and emitted. That sets the input_location to that of the class. But doesn't immediately restore it. Then the next iteration of the main loop in c_parse_final_cleanups discovered a new vtable needed emitting and calls mark_used on the vfuncs it points at. That caused instantiation of the body of a vfunc within the template class whose vtable we were emitting. We still had the stale input_location from the above synthesis. The upshot of which is we think the cause of the template instantiation of the vfunc was at the other class definition, and as that was from a header file, don't think we're in the main input file. I failed at reducing the testcase. Anyway, the fix is to set input_location to something definite before calling mark_used in maybe_emit_vtables. Candidates are: 1) locus_at_end_of_compilation 2) locus of the class owning the vtable 3) locus of the vfunc being marked. I went with #3 in this patch. Although #2 is attractive, the class definition could easily be in a header file, which would fail the is_main_input_context test. This changes the location of the 'required-from' message in g++.dg/template/instantiate5.C. It now points at the vfunc. However, the current location is misleading -- although it's on the 'C c;' line, that's just happenstance as the last token in the file. If we append some unrelated C++, we'll point to that. Which isn't really helpful. committing to trunk. nathan 2018-02-14 Nathan Sidwell gcc/cp/ * decl2.c (mark_vtable_entries): Set input_location to decl's. (c_parse_final_cleanups): Restore input_location after emitting vtables. gcc/testsuite/ * g++.dg/template/instantiate5.C: Adjust required-from loc. Index: cp/decl2.c =================================================================== --- cp/decl2.c (revision 257657) +++ cp/decl2.c (working copy) @@ -1825,6 +1825,11 @@ mark_vtable_entries (tree decl) function, so we emit the thunks there instead. */ if (DECL_THUNK_P (fn)) use_thunk (fn, /*emit_p=*/0); + /* Set the location, as marking the function could cause + instantiation. We do not need to preserve the incoming + location, as we're called from c_parse_final_cleanups, which + takes care of that. */ + input_location = DECL_SOURCE_LOCATION (fn); mark_used (fn); } } @@ -4727,6 +4732,9 @@ c_parse_final_cleanups (void) reconsider = true; keyed_classes->unordered_remove (i); } + /* The input_location may have been changed during marking of + vtable entries. */ + input_location = locus_at_end_of_parsing; /* Write out needed type info variables. We have to be careful looping through unemitted decls, because emit_tinfo_decl may Index: testsuite/g++.dg/template/instantiate5.C =================================================================== --- testsuite/g++.dg/template/instantiate5.C (revision 257657) +++ testsuite/g++.dg/template/instantiate5.C (working copy) @@ -18,7 +18,12 @@ struct B template struct C { - virtual void bar() const { T::foo(); } // { dg-error "no matching function" } + virtual void bar() const // { dg-message "required" } + { + T::foo(); // { dg-error "no matching function" } + } }; -C c; // { dg-message "required" } +C c; + +int k;