Blog post

AutoConfig: C++ Code Analysis Redefined

Abbas Sabra photo

Abbas Sabra

C++ Analyzer Developer

5 min read

  • C
  • C++
  • SonarQube Server
AutoConfig for C and C++ Projects Image

C++ AutoConfig and SonarQube Server

Welcome to the future of code analysis with SonarQube Server 10.6’s AutoConfig, where high-quality, Clean Code is not just an idea. It’s an instant reality for every C and C++ project. AutoConfig eliminates all the usual prerequisites: no specific compiler allegiance, no elaborate setup rituals, and no dependencies on your project’s build environment. Whether you’re working on an embedded project with a specialized, lesser-known compiler or a small, resource-strapped initiative, AutoConfig integrates seamlessly, offering an effortless path to code analysis. It eradicates the complexities of the past, where generating a Compilation Database and ensuring environment compatibility were necessary evils. Now, every developer can immediately start their journey to a cleaner, better code with minimal effort and maximum impact.


From Configuration Chaos to Clarity

Tools requiring an understanding of C and C++ code statically, such as code analysis, refactoring, and IntelliSense, typically ask developers to manually provide detailed configuration information on how their projects are compiled. This crucial setup involves specifying the ‘include’ search directories and macro definitions, which can drastically alter the code semantics by resolving different dependencies and tuning the preprocessor behavior. Each tool has a separate approach, often requiring unique configuration files, leading to a fragmented and labor-intensive setup process.


The advent of the Compilation Database, an innovation by the LLVM project, marked a significant leap forward. It offers a standardized JSON format that describes the compilation commands for each source file, helping unify the configuration process across different tools. However, generating this database is not straightforward and depends heavily on the build system. At Sonar, we designed the Build-Wrapper to simplify generating the Compilation Database by wrapping the build and capturing build commands. Still, it may be incompatible with some projects with restrictive build environments or non-standard toolchains. Alternatively, while CMake can generate a Compilation Database without a prior build, this option remains limited by the necessity for specific CMake generators and compilers. This requirement can render it impractical for many projects unable or unwilling to adapt their toolchain just for code analysis compatibility. Additionally, even with a Compilation Database generated by CMake, the analysis still needs to be performed post-build to account for any generated files and fetched dependencies. This necessity underscores the challenge: while a modern and flexible environment facilitates these requirements, mandating such conditions can be prohibitive. Projects tied to older or less adaptable systems may find this requirement a significant barrier to accessing advanced code analysis tools.


AutoConfig in SonarQube Server 10.6 changes the game by automating the detection and configuration process. By scanning the project’s code and system libraries, AutoConfig applies heuristics to deduce a valid configuration that effectively compiles and analyzes the code, covering the most extensive codebase without needing manual intervention or specific build environments. For users who wish to fine-tune the analysis configuration, AutoConfig offers the flexibility to tune the computed settings through easy-to-use UI properties, making it a versatile and powerful tool for any C or C++ project.


Bridging Compiler Gaps

The world of C and C++ development is vast, populated by an array of compilers, each with its own set of versions, language extensions, and command-line idiosyncrasies. Traditional code analysis tools struggle to support every possible compiler, especially older or domain-specific ones with private documentation and unique behaviors. This limitation has historically left many projects without access to advanced code analysis capabilities.


AutoConfig introduces a groundbreaking solution to this problem. By modeling compiler behavior in a generic, resilient manner, AutoConfig can parse ambiguous code and handle language extensions effectively. It approaches incomplete code by recognizing what is known and treating the unknown as a black box, minimizing the risk of false positives. This strategy ensures that AutoConfig provides reliable static code analysis even in environments where conventional tools fail. As a result, code analysis becomes accessible and easy for projects using less mainstream compilers, democratizing high-quality software development across all domains.


Breaking Free from Environmental Constraints

Traditional static code analysis tools are often hamstrung by the need for an analysis environment that exactly replicates the build environment. This requirement can inflate costs and complicate the integration within existing CI workflows, especially when the build is distributed across different machines or systems. The typical workaround has been to restructure CI pipelines to accommodate these tools, which often means complexifying and centralizing tasks unnaturally.


With AutoConfig, these barriers are dismantled. AutoConfig leverages advanced techniques to emulate compiler behavior and dependency management without needing access to the original build tools or environments. This capability not only facilitates the use of secure, isolated analysis environments like Docker but also makes parallelizing the build and analysis more attainable. AutoConfig’s ability to adapt to various project needs without restructuring entire CI workflows revolutionizes how static code analysis is deployed, making it a seamless part of the development workflow rather than a disruptive one.


One Step to Clean Code

Prior to Sonar AutoConfig, onboarding a project and setting up effective code analysis required navigating a complex maze of configurations and setups tailored to different build systems, operating systems, and CI providers. SonarQube Server alone has over 30 C++ onboarding examples, covering variations using the Compilation Database approach. This highlights how overwhelming setting up and configuring C and C++ projects can be. Each example at SonarSource’s CFamily examples demonstrates the extensive manual setup needed.


Contrast this with the revolutionary simplicity of AutoConfig to start analyzing your C or C++ project. You can simply run the SonarScanner CLI without any preconfiguration. The SonarScanner CLI does not require any C++ specific inputs. It operates seamlessly in the background, automatically adapting to your compiler, configuration, and environment. The process is as straightforward as downloading the SonarScanner CLI and executing it on your codebase. SonarQube Server’s onboarding UI will walk you through a further streamlined process depending on your CI provider. For instance, GitHub users may use a GitHub Action that automates the download and execution of the SonarScanner. Similarly, tailored solutions exist for Bitbucket Pipelines, Azure DevOps, and GitLab, making onboarding virtually effortless across platforms.


Unlocking Clean Code for All

Starting with SonarQube Server 10.6, C and C++ analysis enters a new era with AutoConfig, designed to make code analysis free of complications and more accessible to every project. AutoConfig automates the complex setup process traditionally associated with static code analysis, allowing you to achieve Clean Code with minimal configuration effort. For those who need to fine-tune the analysis, high-level scanner properties are easily adjustable and detailed in the Customizing the Analysis with AutoConfig guide.


While AutoConfig offers a streamlined approach, users requiring more control can still fall back on the Compilation Database mode. To understand the advantages and disadvantages of both modes, visit Choosing the Right Analysis Mode.


We eagerly anticipate your questions and feedback on AutoConfig. Join the discussion and share your experiences on the Sonar Community Forum.


Ready to give AutoConfig a try? Get started with SonarQube Server Developer Edition

Get new blogs delivered directly to your inbox!

Stay up-to-date with the latest Sonar content. Subscribe now to receive the latest blog articles. 

By submitting this form, you agree to the storing and processing of your personal data as described in the Privacy Policy and Cookie Policy. You can withdraw your consent by unsubscribing at any time.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

  • 法律文件
  • 信任中心
  • Follow SonarSource on Twitter
  • Follow SonarSource on Linkedin

© 2008-2024 SonarSource SA。保留所有权利。SONAR、SONARSOURCE、SONARQUBE、 和 CLEAN AS YOU CODE 是 SonarSource SA 的商标。