Clang-tidy doesnt find stdlib

i have the following shell.nix

{ pkgs ? import <nixpkgs> {}, clangVersion ? 17 }:
let
  llvmPackages = pkgs."llvmPackages_${toString clangVersion}";
  customSystemc = (pkgs.systemc.override {
    stdenv = llvmPackages.libcxxStdenv;
  }).overrideAttrs (oldAttrs: {
    CXXFLAGS = [ "-std=c++20" ];
  });
in
(pkgs.mkShell.override { stdenv = llvmPackages.libcxxStdenv; }) {
  buildInputs = [
    pkgs.bear
    pkgs.clang-tools
  ];

  shellHook = ''
    export SYSTEMC_HOME=${customSystemc}
  '';
}

and the following Makefile:

# Compiler and flags
CXX = clang++
CXXWARNINGS = -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic
CXXFLAGS = $(CWARNINGS) -std=c++20 -g -O2

# Include and library paths
INCLUDES = -I$(SYSTEMC_HOME)/include
LIBS = -L$(SYSTEMC_HOME)/lib -lsystemc

# Source files
SRCS = test.cpp

# Object files
OBJS = $(SRCS:.cpp=.o)

# Executable name
TARGET = test

# Build rules
all: $(TARGET)

$(TARGET): $(OBJS)
        $(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $(OBJS) $(LIBS)

%.o: %.cpp
        $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

clean:
        rm -f $(OBJS) $(TARGET)

which lets me compile the following with clang.

#include <format>
int main() {
    return 0;
}

but when i now run

bear -- make
clang-tidy test.cpp

i get

$ clang-tidy test.cpp
1 warning and 2 errors generated.
Error while processing /home/.../systemc/test.cpp.
/home/.../systemc/test.cpp:1:10: error: 'format' file not found [clang-diagnostic-error]
    1 | #include <format>
      |          ^~~~~~~~
/home/.../systemc/test.cpp:2:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
    2 | int main() {
      | ~~~ ^
      | auto       -> int
Found compiler error(s).

I can kinda fix this by manually including -I${llvmPackages.libcxx.dev}/include/c++/v1 in the compile_command.json but that’s ofc not ideal and also makes clang-tidy analyze stdlib files, which results in a bunch of annoying errors.

How do I make clang tidy aware of the stdlib header files?

I think this is a continuation of Clang tooling woes 2 - Help - NixOS Discourse

2 Likes

here a repo to make it easier to replicate this zeratax/nix-shell-clang-tidy (github.com)

Not related to clang-tidy but: I think clang-tools should be overriden with Clangd issues on nixos: missing headers - #9 by gabyx

          # C++
          # Clangd from clang-tools must come first.
          (hiPrio clang-tools.override {
            llvmPackages = llvmPackages_16;
            enableLibcxx = false;
          })
          # Do not use the clangd from this package as it does not work correctly with
          # stdlib headers.
          llvmPackages_16.libstdcxxClang

but I guess since you overwrite stdenv in mkShell I guess pkgs.clang-tools takes that, not sure. I guess it does not.

I think clang-tidy has the same troubles as clangd… I also need this somehow fixed…