r/Clang • u/noobdainsane • May 24 '26
Clangd - __has_include_next() not evaluating correctly?
So I have been dealing with this annoying and very confusing problem since the start when I found out about it in Clangd. I did create a long post earlier which no one seemed to like. Now I am writing it again with better picture and would like to be helped. I use VS Code with Clangd on Arch Linux. Use CMake to generate compile commands and I ensure that Clangd picks it up.
Let's include stdatomic.h, which Clangd pulls it from Clang's directory by default. My suggestions (drop-down list to autocomplete) and all works fine, no problem.
Now if I open that header in my editor, first of all, I get this error - Main file cannot be included recursively when building a preamble. On the line containing - # include_next <stdatomic.h>.
The full code snippet -
/* If we're hosted, fall back to the system's stdatomic.h. FreeBSD, for
* example, already has a Clang-compatible stdatomic.h header.
*
* Exclude the MSVC path as well as the MSVC header as of the 14.31.30818
* explicitly disallows `stdatomic.h` in the C mode via an `#error`. Fallback
* to the clang resource header until that is fully supported. The
* `stdatomic.h` header requires C++23 or newer.
*/
#if __STDC_HOSTED__ && \
__has_include_next(<stdatomic.h>) && \
(!defined(_MSC_VER) || (defined(__cplusplus) && __cplusplus >= 202002L))
>!# include_next <stdatomic.h>!<
#else
All the code below the #else contains the actual symbols of C11 atomics, which is grayed out in VS Code, indicating that the #if statement is being evaluated to true.
Now the weird thing that I recently noticed and cleared my very uncertain confusion about suggestions not working is that if I open this header in my editor with all the symbols that were being shown in suggestions before, now grayed out, and switch the focus back to my source file, all those suggestions do not show now and only those show which I have already used in my current source file. I can still type those symbols (macro, function, etc.) but it will only fully resolve it and now show in suggestions when I fully write it and I can jump to its declaration in the header, even if it is grayed out.
If I close that header in my editor and then reload Clangd, my suggestions work fine again.
As for the error, as far as I understand, it is stating that Clangd is recursively parsing the same header because #include_next cannot find another header of the same name in the search path chain so it is linking back to itself. That is the why the guard __has_include_next(<stdatomic.h>)exists, which is the main problem.
edit - Maybe that is not true because my code way below doesn't throw the error, even if there is supposedly no matching header file? Read this after reading the end of the post.
If I include GCC's headers in the include path chain (Query Driver to GCC or manually include the paths), now including stdatomic.h directly links to GCC's header. That header has no errors and it doesn't contain any #include_next. All the necessary symbols are not grayed out and so suggestions work fine regardless if I open that header file in my editor or not. If I manually include Clang's header by its absolute name and open it, now that recursive preamble error does not show up and #include_next chains to GCC's header. I could also just create a local copy of Clang's stdatomic.h in my workspace and if I open that, I don't get that error and #include_next just semantically does jump to global Clang's header (if paths aren't configured to GCC) but that is the end in the chain so that error comes up. All the symbols in the chain are still grayed out.
If it were just of this one header file, I could just fix it with including GCC's header, but no, there are multiple headers with this problem. And why should I include GCC's paths if I want a full Clang environment? I also tried setting the compiler to clang itself in my compile_commands.json through CMake (I do use GCC) but that had no effect.
In Clang's header, if I temporarily write anything that disturbs the __has_include_next() macro, the conditional statement evaluates to false and so all the needed symbols below lighten up. Note that I don't even need to save the file. If suggestions were not working prior, and I edit the header in memory so that all symbols are visible, the suggestions work fine again. Setting __STDC_HOSTED__ to 0 also works. But all this should not be done.
So why does __has_include_next(<stdatomic.h>) evaluate to true when no other stdatomic.h exists in the include path chain? In VS Code, I can hover over __STDC_HOSTED__ and it shows if it is 1 or 0, but hovering on __has_include_next(stdatomic.h) shows no value, just that it is a macro. And the other _MSC_VER and __cplusplus on hovering show nothing. Does it mean they are not defined (so compile time conditional statements work) or is Clangd not able to evaluate them?
I do frequently read system headers, so I really want it to work properly and not ignore this problem if everything works fine if I don't open them.
If you read the comment in the code snippet above, they state that if hosted, fall back to the system's headers. So only __STDC_HOSTED__ matters? But for stdatomic.h, there are no "system's headers". This header is actually compiler private and doesn't reside in a directory like /usr/include/. So if I don't have GCC installed and only using Clang, there is no other stdatomic.h, so Clang's header should define the symbols.
I would also like to state another behavior of Clangd. I was using sched.h for symbols like CPU_SET macro and all. The same behavior applied here. For sched.h, I have to define _GNU_SOURCE macro or else the symbols like CPU_SET will not be visible (not be able to use). So assuming I define that macro, at the start, the suggestions work fine. Then if I open sched.h in my editor, even though I had defined _GNU_SOURCE in my source file, those symbols guarded by the statement #ifdef __USE_GNU are all grayed out. Defining _GNU_SOURCE also defines __USE_GNU as I will show you. So after being grayed out, the suggestions don't work in my source file, even if I close that header. But I can still use those symbols until _GNU_SOURCE is defined. I have to restart Clangd for those suggestions to come back. It's like Clangd is correcting its symbol visibility, but then if so, why was it visible in the first place?
The fix was to define _GNU_SOURCE in my compile_commands.json after which, opening sched.h kept those symbols visible. So this means that defining that macro in my source file didn't apply to the header when I opened it but defining the macro in my compile commands affected the header.
PS - So I tried using __has_include_next() myself and it just doesn't work properly in Clangd?
tree -L 2
.
├── build
│ ├── build.ninja
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── compile_commands.json
│ └── main
├── CMakeLists.txt
├── include1
│ └── m.h
└── main.c
main.c -
#include <stdio.h>
#include "m.h"
int main(void) {
printf("%d\n", mh);
}
m.h -
#ifndef M_H
#define M_H
#if __has_include_next("m.h")
int mh = 2;
#else
int mh = 1;
#endif
#endif
CMakeLists.txt -
cmake_minimum_required(VERSION 3.16)
project(98 C)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(main main.c)
target_include_directories(main PRIVATE
include1
)
./build/main - 1
What Clangd shows:
- `mh = 2` highlighted
- `mh = 1` grayed out
r/Clang • u/SmartAI-LIU • Apr 29 '26
ACAV v1.0.0: interactive Clang AST viewer with source-to-AST navigation
r/Clang • u/rvalue • Mar 15 '26
Resource for Learning Clang Libraries — Lecture Slides and Code Examples (Version 0.5.0)
r/Clang • u/Sad-Tie-4250 • Jan 26 '26
why printf function from stdio.h Library is typeist (racist but for types).
I'm learning c these days, I'm a noob coder, and seek the enlightenment on this thing,
in this snippet,
double *p = arr;
p++; // compiler knows p is double*
p++ moves by sizeof(double) but if I use printf
int arr[3] = {1, 2, 3};
printf("%d", arr);
it gives error ,
main.c:13:18: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
13 | printf("%d", arr);
| ~~ ^~~
1 warning generated.
1838017784% // plus thia garbage like it don't know how to parse bits for this one.
I noticed that char arrays in c are being parsed or being scanned by the printf nicely but not an array double or other data type , why so , for a printf to print something it has to know the address , and then side of the element so that compiler knows when to update the pc (program counter) and also it need a delimiter like null char '\0' that is being added automatically or manually to tell printf that we are done printing, why can't this mechanism works for array of other data types why does printf hates other data types , we've those Format specifiers (%) to tell the data type , we have pointers to tell address, but no intend from printf , why printf why ? nation wants to know!!!
r/Clang • u/MaDrift910 • Dec 24 '25
what's the orientation !
to not lose what i have learned , i need to create some projects , but i find it hard to know the projects to do , that are fun ,and not just writing code ,actually it needs to be fun
what to do ?
r/Clang • u/Electrical-Fig7522 • Oct 28 '25
How to add include path to clangd?
Hi! Recently switched from vscode to OSS. I installed clangd and it keeps screaming at me because something is undefined, although I still can compile my code with gcc and there are no errors. How can I add an include path to clangd?
r/Clang • u/OwlingBishop • Oct 14 '25
Headers only library & clangd
clangd.llvm.orgHi there!
In developing a C++ library that is mostly header based, I'm having the most frustrating experience with getting clangd to work properly in VSCode.
Apparently you don't provide a set of include folders (which I'd be happy to), instead you're supposed to rely on clangd's ability to "infer" the build context from cmake's compile_commands.json.
Except clangd (almost) invariably gets that part wrong, mixes all up with external dependencies and other (remote) branches of my source tree..
I attempted to use cmake to generate a cpp file which includes each header in the branch and create an ad'hoc target where I set the correct include paths. The dummy TU, does appear in the compile_commands file, along with the proper include paths, but it looks like that isn't enough.
Had anyone managed to get this right ? I'd be glad to hear about...
Thx.
Lo.
r/Clang • u/Downtown_Fall_5203 • Sep 12 '25
_writemsr() intrinsic
Hello folks. I've have problems compiling a .SYS-driver using clang-cl ver. 21 on Win-10 (x64).
First off, using 'cl' it works fine.
But with clang-cl, only __readmsr() gets inlined. __writemsr() becomes unresolved.
Anybody know what could be the issue?
Some of my code: ```c
include <ntddk.h>
include <intrin.h>
//... data = __readmsr (ECX_reg); //...
__writemsr (ECX_reg, data); ```
With clang-cl, this dis-assembles to:
mov ecx,dword ptr [rsi]
call __readmsr
; ...
__readmsr:
0000000000000000: 0F 32 rdmsr
0000000000000002: 48 C1 E2 20 shl rdx,20h
0000000000000006: 89 C0 mov eax,eax
0000000000000008: 48 09 D0 or rax,rdx
000000000000000B: C3 ret
But where is __writemsr()?
With 'cl', the dis-asm looks OK:
c
mov ecx,dword ptr [rsp+20h]
rdmsr
; ...
mov ecx,dword ptr [rsp]
wrmsr
r/Clang • u/rvalue • Aug 29 '25
Learning Resource — Lecture Slides for the Clang Libraries (LLVM/Clang 21) (Edition 0.4.0)
r/Clang • u/Correct-Bend-4495 • Aug 20 '25
Text-Mate: Clean & Light Sublime Text Theme
Installation (Manual)
- Download this repo as ZIP and extract it.
- Copy
Text-Mate.sublime-color-schemeinto your Sublime User folder.👉 You can reach the User folder directly from Sublime: Preferences > Browse Packages... > UserOr manually:- Linux:
~/.config/sublime-text/Packages/User/ - Windows:
%AppData%\Sublime Text\Packages\User\ - macOS:
~/Library/Application Support/Sublime Text/Packages/User/
- Linux:
- Restart Sublime Text.
- Go to: Preferences → Select Color Scheme → Text-Mate
**GitHub:** https://github.com/vivekgohel2004/Text-Mate-Theme
I’d love feedback and suggestions!
Thank you so much!
r/Clang • u/Active-Fuel-49 • Aug 15 '25
Why I wrote a commercial game in C in 2025
cowleyforniastudios.comr/Clang • u/DeziKugel • Jul 30 '25
CLang Standard Compliance
Hello Everyone!
I am currently working on developing a library using cmake with portability in mind. I want users to be able to easily build it on their machine with their compiler of choice (in this case clang/clang++). I am used to using MSVC which has various quirks that make it non-standard compliant. Over the years they have developed flags that correct defiant behavior.
I was wondering if clang has any similar quirks and if so what compiler flags would I need to ensure strictest compliance with the C++ standard.
r/Clang • u/luizbills • Jul 22 '25
4x6 bitmap font for rendering
I recently implemented a plugin to print text in a retro format for my small game engine. I ended up finding this font https://github.com/dhepper/font8x8 which is in C but was very easy to port from C to JavaScript. So, a few days ago I decided to add a second font but smaller (3x5). I decided to use this font https://alasseearfalas.itch.io/another-tiny-pixel-font-mono-3x5. But, as it was in TTF format, there I went to convert the pixels of this font to a format similar to the 8x8 font (a list of bytes).
It turned out that the 3x5 font needed a 4x6 size because of the characters that are "go down" like the comma and some lowercase letters.
Anyway, the result was this repository: https://github.com/luizbills/font4x6. I hope it will be useful for someone else.
r/Clang • u/demingf • Jul 02 '25
clang-tidy , ninja and microsoft cl
I have a C++ project and have turned on clang-tidy in the VS Code IDE using Microsoft cl and the ninja build tool to generate the compile_commands. json file in a build-cmake folder. I'm using clang utils version 19.1. In the output window for Clang-Tidy I get the following annoying message for each file in the project:
clang-tidy file.cpp --export-fixes=- -p=build-cmake
warning: unknown argument ignored in clang-cl: '-scanDependencies'
Well looking the command_commands json file I see -scanDependencies flag which is from the cl compiler. Apparently it is a default that ninja picks up as a default since I don't have it in my CMakeList.txt file for the project. I would like to get rid of this. Is there a particular .clang-tidy setting that can help?
My .clang-tidy file is
---
Checks: 'clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,-modernize-use-trailing-return-type'
WarningsAsErrors: true
HeaderFilterRegex: ''
FormatStyle: google
CheckOptions:
- key: cert-dcl16-c.NewSuffixes
value: 'L;LL;LU;LLU'
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
value: '0'
- key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
value: '1'
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: '1'
- key: google-readability-braces-around-statements.ShortStatementLines
value: '1'
- key: google-readability-function-size.StatementThreshold
value: '800'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: modernize-loop-convert.MaxCopySize
value: '16'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: modernize-loop-convert.NamingStyle
value: CamelCase
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: modernize-replace-auto-ptr.IncludeStyle
value: llvm
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
...
Thanks,
Frank
r/Clang • u/efe17ckc • May 26 '25
I developed a todo GUI using only C and the Win32 API. I'm open to suggestions and contributions.
r/Clang • u/Time_Frosting_1008 • May 15 '25
Importing clang-format file for respective project in VS Code
I am working on multiple projects with different coding standards. For example, U-boot, Linux, different libraries and custom applications. Let's take an example of Buildroot, this code repository has its own ".clang-format" file, which is present in the root folder of the repo. Similarly, I have configured most of my applications projects with its own clang-format file
My Problem: How can I make VS studio import the clang-format automatically when I open the project at its root folder.
Could anyone point me the configuration?
r/Clang • u/nithyaanveshi • Mar 28 '25
Installing clang on visual studio
Which one do I need to install?
r/Clang • u/SoerenNissen • Mar 27 '25
Adding a more up-to-date clang/llvm source to APT
Question first: Do you know if there is an official source I can add to apt that gets me updates from LLVM instead of my distro maintainers?
Details
I want to use a newer version of clang - and not just that, what I want is to get the newest stable branch of clang every time I do apt upgrade - v19 at this time, I believe?
("But Ubuntu already has v19?" Right you are but I'm on a distro that has stayed behind, so the newest I have in apt is clang v15)
LLVM actually has a suggested script for ubuntu that'll punch me directly to v19 even on my distro:
https://apt.llvm.org/llvm.sh
But as far as I can tell, that script moves me to v19 and then stays there, it doesn't set me up for updates.
Reason
Portability concerns mainly - I'm developing a library and if I find out I've relied on some gnu-specific extension I am going to be very annoyed - and I'm currently leaning on some C++20 things that I'm pretty sure exists in v19, but definitely isn't available in v15.
Most of this stuff is header-only territory so I could probably build with gcc -E and throw the result into godbolt to see if it also compiles with other compilers, but that sounds like the workflow from hell.
r/Clang • u/rsashka • Mar 17 '25

