Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp786877rwr; Wed, 19 Apr 2023 13:06:55 -0700 (PDT) X-Google-Smtp-Source: AKy350bQids2UtliIC7KRaP6w/sdmkbQNvWQ/yRS9CJQbjHjMO8HWdmLLqY11qmHnzKwwIBkln7A X-Received: by 2002:a05:6a00:24c9:b0:62d:bf69:e9e0 with SMTP id d9-20020a056a0024c900b0062dbf69e9e0mr4267546pfv.17.1681934814801; Wed, 19 Apr 2023 13:06:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681934814; cv=none; d=google.com; s=arc-20160816; b=KEQzEfGR+Avd2Peq+/pJ++PuEEunMnVrAtc2Uuhthai+NcSlGQjmg+vtnfzWv71lLC rqLoWO17vO4cxJGuDnU/4jf6LaF1T563zl8mo12Ar1lz9nPbieAoEQ88DgC/gbjUn9un hgZkzrEVhpo9hkoHV69HIHb7Ge4alqYwdFysK0ZUbeR84yqq2yuvHfO1oKkxv3Mkyx6r CgL73WLBgb36RIMypPcFlppvPJu3Ys8OcEmMI5oIzE+DlbmgasO3PSS3QiWLXJHTPz9R BMWX31/4gCD2XGA5RYIlyOUCYUPpQ3xykCcfCSn9p/kKiZXf6rbCWMAa96J2DArBv7YD Th6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=56W2W/X8fkj8VZX3/1DTAaK9+j9FvXQXuYPz0XAniuc=; b=A2QmyBesyUhlaevWQVaZxVxvXhR3b52rZNWyr66UjtUy22Q19WSBzMJIOYajtRCbf9 ehBhbbu67It6tZ9PAt2fiyzNO7YErBQSyIH34MS0Ce9QmweeRpw3rEKAcD73PZhnJFyE kC8CmbkcN3w2usY2Xk8qB6kTM44KxTLQTHRWdPqI6PHLsU9iV2A65pBgLYmoGBKvd1cq X/Q+8pjjzpd8tQGLioDCPIrKPdqPSy9oL7cCSeTNejDMqwqdQHsItkjOnP0/25Cw4bUD AiV7lOFC9HqVLOTozYEx0nrpr5M83oFJKr0+QrmrpX3zhNxWb9ugxImdnPDOBxn+hGhH HSCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Tm+PqrTi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r27-20020aa79edb000000b005dd4ab3a2c3si4610631pfq.182.2023.04.19.13.06.42; Wed, 19 Apr 2023 13:06:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Tm+PqrTi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229785AbjDSUCR (ORCPT + 99 others); Wed, 19 Apr 2023 16:02:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229514AbjDSUCP (ORCPT ); Wed, 19 Apr 2023 16:02:15 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6FD0468C for ; Wed, 19 Apr 2023 13:02:14 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1a652700c36so232875ad.0 for ; Wed, 19 Apr 2023 13:02:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681934534; x=1684526534; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=56W2W/X8fkj8VZX3/1DTAaK9+j9FvXQXuYPz0XAniuc=; b=Tm+PqrTiBoRJA04/Kl60G2CqwjByx5la8XcHF9okQZRzsClYBxiJTHt0YG5c7Ih8mT LqiugknROCpyehmf+fFE2yLXzkxa7RRDDerSCmGNO6gj8wHE1MUCc2qXLU1NwoxWHQTN PB/mAP8j6XUow6402YryLB5jYviHopAJ4iYiURfF9qoPqv16Riw+InNihTXtwAWmPte2 WzvgnJ1CLUIbSsA3WkAuSE2s+YmMzyBgHqf77zkPrj56JYAOdVHMo01j2j2szEV7YTIA 39nPjEODCVxQsypbfnaq1e0jji5goKJ9EUboMoqyesJEob+LeW9h6gbLFLLBA+vTiinI dJgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681934534; x=1684526534; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=56W2W/X8fkj8VZX3/1DTAaK9+j9FvXQXuYPz0XAniuc=; b=SnldDf3qOyRO9HC80ZAbBXApkUaFeuaObHBYGawYdzD+WnwCT6LE0rjXxNu5Bigw/G ndtSZdGHPHSCbLpuT7xE9hYe/FqIoohJP7mT94HXVhGupdRcExE5gDt/Xhs2sd5JrCsF gjxFQyRFvTlljRwt/2PP07vYqTRxD3X3y10HNhNdTYYwoz1ca2qWSKG7ZMSTtE3/UZ9d hIc0PKSKakNrfJbd0JmIUYLA1z6MkikXZS8/nNlELJr9UBlEL4eQrkfuB3w9ARqSs81/ EaRqI+ARVPRsRfqBjFKuqMkTU6B7LLtaxl9BmSdzWFZj776lulw+v75M46gd13zHffxh wiYQ== X-Gm-Message-State: AAQBX9e8Kwq+WfVqAHsHdEz3FQ5FKWdYPPl0EVT8JBgIBeShmsjvyrM7 Ggb9GK7e2yin9ANuddz6Qnzaf8eZNIUOFs+Sr7v/1vhzd0Vz079ozt7VyQ== X-Received: by 2002:a17:903:228c:b0:198:af4f:de12 with SMTP id b12-20020a170903228c00b00198af4fde12mr49157plh.18.1681934534077; Wed, 19 Apr 2023 13:02:14 -0700 (PDT) MIME-Version: 1.0 References: <20230414005309.GA2198310@google.com> In-Reply-To: From: Fangrui Song Date: Wed, 19 Apr 2023 13:02:02 -0700 Message-ID: Subject: Re: clangd cannot handle tree_nocb.h To: Florent Revest Cc: Nick Desaulniers , Joel Fernandes , rcu@vger.kernel.org, nathan@kernel.org, trix@redhat.com, llvm@lists.linux.dev, linux-kernel@vger.kernel.org, paulmck@kernel.org, "revest@chromium.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 19, 2023 at 6:00=E2=80=AFAM Florent Revest = wrote: > > On Tue, Apr 18, 2023 at 6:28=E2=80=AFPM Nick Desaulniers > wrote: > > > > + Florent > > Hi there! > > > Joel, Florent is doing some cool stuff with clangd and kernels (check > > out the demo at go/linux-kernel-vscode). I'm pushing Florent to > > Apologies for folks outside Google, this is an internal link to a > kernel dev setup I originally created for myself, then for my team and > apparently more and more people are starting to use it internally. :) > If there's enough appetite for it externally too, I'll try to > open-source it someday. Anyway, in the context of this conversation, > it's just something that uses clangd. :) > > > publish some of the cool stuff he's been working on externally because > > it is awesome. ;) > > > > Florent, have you seen any such issues such as what Joel reported below= ? > > Yes, I've seen this problem a bunch of times. Afaiu, Clangd operates > under the assumption that every source file is a valid compilation > unit. My understanding is that it's generally a good practice to keep > things that way and I wouldn't be surprised if the userspace Chrome > code-base Joel saw enforces this (iirc, it's a rule for > Google-internal C++ too, all headers must be interpretable > independently). > > However, from the perspective of the C spec, anything can be included > anywhere and a C file can only make sense if it's included > after/before certain other things are defined/included. Spontaneously, > I would call these ".inc" rather than ".h" or ".c" because I would > expect a source file to be always valid and this suffix makes it > clearer they depend on their context, but as a matter of fact source > files that don't compile when interpreted individually are quite > common in the kernel tree. Other examples that have been reported to > me include a lot of kernel/sched/*, since many of these files > (including .c files) are included from kernel/sched/build_policy.c in > a specific order to form one big compilation unit. > > Unfortunately, I don't know of any solution. :( This feels like a > limit of C or compile_commands.json to me. "compile commands" can not > be enough to interpret any file, clangd would need a way to express > "interpret this file as if it were included in that spot of that > compilation unit" and maybe even need a bunch of heuristics to choose > one such include spot. > > I don't know if clangd has any plan to address this and so far I've > just lived with these error squiggles. > Some information about Clang based language servers It's good practice to ensure that a header file is self-contained. If not, for example, if a.c includes a.h, which is not self-contained, a.h is generally compiled on its own (as if using clang [options] a.h) to confine diagnostics/completion results to a.h and not to other headers included by a.c. However, this design choice may cause language servers to emit diagnostics that you don't see when building the project. For my tool ccls, the index file for an included file (e.g. a.h) is computed when compiling the main source file (a.c). Therefore, if the project builds successfully, the index is always accurate. Language servers usually have the limitation that only one configuration of a main source file is recorded, e.g., you only get index data for either clang -DA=3D1 a.c or clang -DA=3D2 a.c, not both. This limitation exists due to technical challenges and practicality. If a main source file has 10 configurations, when you edit the file, do you compile it 10 times to get all indexes? --- My setup is like this. I have been doing it since about 2019, though I rarely read Linux kernel code :( Ensure you have the following Clang and LLVM tools. If you build them by yourself, create a llvm-project build directory, say, /tmp/Rel ``` ninja -C /tmp/Rel clang lld llvm-{ar,nm,objcopy,objdump,ranlib,readelf,strings,strip} ``` In a Linux kernel source directory, (https://github.com/MaskRay/ccls/wiki/Example-Projects) ``` PATH=3D/tmp/Rel/bin:$PATH make O=3D/tmp/out/x86_64 ARCH=3Dx86_64 LLVM=3D1 d= efconfig PATH=3D/tmp/Rel/bin:$PATH make O=3D/tmp/out/x86_64 ARCH=3Dx86_64 LLVM=3D1 -= k bzImage modules # generate ..cmd files scripts/clang-tools/gen_compile_commands.py -d /tmp/out/x86_64 ``` emacs -nw fs/binfmt_elf.c # check out lsp-mode and eglot For neovim, use https://github.com/neoclide/coc.nvim or the built-in client. I prefer coc.nvim. --=20 =E5=AE=8B=E6=96=B9=E7=9D=BF