Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Get started with clang-tidy in Red Hat Enterprise Linux

April 6, 2021
Tom Stellard
Related topics:
CI/CDLinuxSecurity
Related products:
Red Hat Enterprise Linux

    Clang-tidy is a standalone linter tool for checking C and C++ source code files. It provides an additional set of compiler warnings—called checks—that go above and beyond what is typically included in a C or C++ compiler. Clang-tidy comes with a large set of built-in checks and a framework for writing your own checks, as well.

    Clang-tidy uses the same front-end libraries as the Clang C language compiler. However, because it only takes source files as input, you can use clang-tidy for any C or C++ codebase no matter what compiler you are using.

    This article is a quick introduction to code analysis with clang-tidy, including how to check for rule violations in a simple C-based program and how to integrate clang-tidy with your build system.

    Using clang-tidy in Red Hat Enterprise Linux

    In Red Hat Enterprise Linux (RHEL), clang-tidy is included as part of the LLVM toolset:

    # RHEL7
    $ yum install llvm-toolset-10.0-clang-tools-extra
    
    # RHEL8
    $ yum install clang-tools-extra
    

    The best way to get started with clang-tidy is to review the list of included checks to see which ones might be useful for your codebase. The Clang-tidy project page includes a summary of the different kinds of checks available. You can see a list of the individual checks available by running:

    $ clang-tidy -checks=* -list-checks
    

    Today, we are going to focus on the checks for the SEI CERT Secure Coding Standard, which are denoted in clang-tidy by the cert- prefix.

    Note: The SEI CERT Secure Coding Standard is maintained by the computer emergency response team (CERT) for the Software Engineering Institute (SEI).

    Checking for errors with clang-tidy

    The following example program violates two of the CERT Secure Coding Standard rules, ENV33-C and ERR34-C:

    #include <stdlib.h>
    
    int string_to_int(const char *num) {
      return atoi(num);
    }
    
    void ls() {
      system("ls");
    }
    

    Let's see what happens when we run clang-tidy on this code:

    $ clang-tidy -checks=cert-* -warnings-as-errors=* cert-err.c
    
    2 warnings generated.
    cert-err.c:4:10: error: 'atoi' used to convert a string to an integer value, but function will not report conversion errors; consider using 'strtol' instead [cert-err34-c,-warnings-as-errors]
      return atoi(num);
             ^
    cert-err.c:8:3: error: calling 'system' uses a command processor [cert-env33-c,-warnings-as-errors]
      system("ls");
      ^
    

    The -checks=cert-* option tells clang-tidy to enable all of the CERT Secure Coding Standard checks, and the -warnings-as-errors=* option tells it to treat all warnings as errors. The -warnings-as-errors takes a wildcard argument, so you can choose which warnings to promote to errors. For example, if you wanted to enable all checks, but only generate an error on the CERT checks, you could do this:

    $ clang-tidy -checks=* -warnings-as-errors=cert-* cert-err.c
    

    Integrate clang-tidy into your build system

    In addition to manually running clang-tidy on your source files, you can also integrate the tool into your build system. Build integration makes it easier to automate the checks and include them in a continuous integration (CI) system. A simple way to integrate clang-tidy into your build is to run the checks as part of a check target, using make or a similar build tool.

    Looking back at our previous example, we can construct a simple makefile with a clang-tidy integration:

    SOURCES=cert-err.c
    OBJS=cert-err.o
    
    all: $(OBJS)
    
    %.o: %.c
            $(CC) -c -o $@ $< $(CPPFLAGS) $(CFLAGS)
    
    check: $(SOURCES)
            clang-tidy $(CPPFLAGS) -checks=cert-* --warnings-as-errors=* $(SOURCES)
    

    Now, we can run ourclang-tidy checks using make check.

    Using a compilation database

    If you are using a CMake base build system, clang-tidy can employ a compilation database. That way, you don't need to manually pass the same CPPFLAGS you used while compiling. To generate the compilation database, you just need to pass the -DCMAKE_EXPORT_COMPILE_COMMANDS=ON option to CMake when configuring. Here's an example CMake configuration for using the compilation database:

    set(sources ${CMAKE_SOURCE_DIR}/cert-err.c)
    
    add_library(cert-err ${sources})
    
    add_custom_target(
        clang-tidy-check clang-tidy -p ${CMAKE_BINARY_DIR}/compile_commands.json -checks=cert* ${sources}
        DEPENDS ${sources})
    
    add_custom_target(check DEPENDS clang-tidy-check)
    

    When you configure with CMake, it will generate a file called compile_commands.json, which clang-tidy uses to determine which compiler flags to employ:

    $ cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    $ make clang-tidy-check
    

    Conclusion

    This was a basic introduction to clang-tidy, but there is much more that you can do with it. For more information, read the upstream Clang-tidy project page.

    Last updated: October 7, 2022

    Recent Posts

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.