﻿# Setting up static analysis for Unreal Engine project

The PVS\-Studio analyzer's been supporting the analysis of the Unreal Engine projects for a long time\. Since the last post on the topic, we've hammered away at this mode\. That's why we'd like to explain how you can quickly set up the analyzer to check your Unreal Engine project\.

![1110_UnrealEngine_project_configuration/image1.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image1.png)

Let's look into a real example of a UE project check to demonstrate how static analysis works\. As the test project, we'll take Lyra, a sample game project that Epic Games has released with Unreal Engine 5\.0\. Lyra is a multi\-module project that is used to demonstrate the UE5 features\.

We work with the project on Windows and build it using Unreal Engine 5\.3\.2\. Let's analyze Lyra using PVS\-Studio 7\.29\.

## How to install PVS\-Studio

The PVS\-Studio installer is on [this page](https://pvs-studio.com/en/pvs-studio/download/)\. To analyze Unreal Engine projects in the way described in this article, you need an Enterprise license\. You can get a trial Enterprise license [at this link](https://pvs-studio.com/en/pvs-studio/try-free/)\. The installation process is quite simple: run the installer and follow its instructions\. You only need the Visual Studio 2022 plugin as an additional component\. The installation process is described in the [documentation](https://pvs-studio.com/en/pvs-studio-quickstart-cppwindows/)\. You can request and enter a license directly in the installer\.

## How to run analysis

The analysis of UE projects differs from the analysis of other projects\. If you try to run the analysis using standard methods, for example, via the Visual Studio plugin, you'll see a warning that the analyzer can't run it\.

This is because the analysis of UE projects is arranged differently\. _UnrealBuildTool_ is used to build the project, and the analysis is run through it\. To start a static analysis of the project \(rather than to build it\), it's necessary to configure the project itself\.

There are three options to configure the Unreal Engine project: 

1. pass flags directly to _UnrealBuildTool_;
1. set parameters in the _\.target\.cs_ files;
1. enable the settings in_ the BuildConfiguration\.xml file _\(usually found_ _in_ %appdata%\\Unreal Engine\\UnrealBuildTool\)\._

Each option has unique features and is suitable for different scenarios\.

The first option enables you to set up a specific configuration and command \(**Build/Rebuild**\)\. The second option allows you to configure a certain target type\. You can learn more about targets [here](https://docs.unrealengine.com/5.2/en-US/unreal-engine-build-tool-target-reference/)\. The third option makes it possible to set global settings for UE projects\.

Let's start with the first option, and then I'll tell you how to use the second one\.

To run the analysis, pass a special flag to _UnrealBuildTool_:

```cpp
-StaticAnalyzer=PVSStudio
```

In Visual Studio, add this flag to the _NMake_ command of the project and run the build\. To add the flag, open the project properties, select _NMake_ and type the flag in the **Build Command Line** field\. 

![1110_UnrealEngine_project_configuration/image2.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image2.png)

The analysis replaces the project build\. If you add the analyzer flag to the _Rebuild_ command, the entire project will be analyzed each time the command is called\. If you modify the _Build_ command, only modified files will be checked when you run the command\. 

For more information on how to run the build and analysis for the project, see the [documentation](https://pvs-studio.com/en/docs/manual/0043/#ID5219D77B4B)\.

You can run the analysis using _NMake_ if you open the Unreal Engine project through the _\.sln_ file\.

JetBrains Rider has an option to open the UE projects directly via the _\.uproject_\. In this case, the _NMake_ command modification isn't available, so we need to default to setting the **StaticAnalyzer** parameter in the _\.target\.cs_ file\.

By default, the report isn't opened in the PVS\-Studio plugin window when analyzing the UE project\. To load the analysis report into the plugin table, enable the **AutoloadUnrealEngineLog** setting before running the analysis\. The report is saved in the project subfolder: _Saved/PVS\-Studio_ \(in Lyra, it'll be _"\\LyraStarterGame\\Saved\\PVS\-Studio\\LyraGame\.pvslog"_\) 

So, you run the analysis and you see the following: 

![1110_UnrealEngine_project_configuration/image3.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image3.png)

The modules are analyzed, not the project source files\. 

Additionally, the table contains warnings related to the Unreal Engine core, not the project\.

![1110_UnrealEngine_project_configuration/image4.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image4.png)

Let's see how to get a more relevant report for the project\.

## How to configure analysis

If you don't configure the analysis in advance, the analyzer will check the auto\-generated files\. It's pointless to work with warnings in such files, and when you analyze them, you need extra time\. Therefore, you can safely disable their check: use the **Don't Check Files \> PathMasks** item in the PVS\-Studio plugin\. Let's add a mask to it: 

```cpp
*.gen.cpp
```

Another issue with the analysis not being configured is the analysis of the Unreal Engine core source files\. If we developed a plugin for UE or modify the core itself, such a check would be necessary\. But right now, we're only interested in checking the project\. So, add the path to the Unreal Engine core to the _PathMasks_ setting\. By default, the path masks corresponding to the standard Unreal Engine path are disabled in the settings\. If you use Unreal Engine from GitHub, add the correct mask\.

![1110_UnrealEngine_project_configuration/image5.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image5.png)

After restarting the analysis, we get this report:

![1110_UnrealEngine_project_configuration/image6.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image6.png)

There are fewer warnings and now you can easily handle them\.

## How to set up via \.target\.cs

Let's explore the second way of running PVS\-Studio\.

Like other settings, you can set up the analysis run in the _\.target\.cs_ files\. This option is most handy if you have to frequently regenerate project files\. In this scenario, modifying the _NMake_ commands won't work because they won't be saved during the solution regeneration\. 

To integrate PVS\-Studio with UE, add the _StaticAnalyzer_ parameter with the _StaticAnalyzer\.PVSStudio_ value to the _\.target\.cs_ file\.

In the _LyraGame\.Target\.cs_, it'll look like this:

```cpp
public class LyraGameTarget : TargetRules
{
  public LyraGameTarget(TargetInfo Target) : base(Target)
  {
    Type = TargetType.Game;
    
    ExtraModuleNames.AddRange(new string[] { "LyraGame" });

    StaticAnalyzer = StaticAnalyzer.PVSStudio; 

        LyraGameTarget.ApplySharedLyraTargetSettings(this);
  } 
  ....
}
```



In the _\.target\.cs_ file, you can set several more PVS\-Studio\-specific settings, for example, enabling diagnostic groups or the analysis timeout\. You can learn more about the settings in the [documentation](https://pvs-studio.com/en/docs/manual/0043/#ID6850AE8285)\. The same settings can be set in the PVS\-Studio plugin interface\.

![1110_UnrealEngine_project_configuration/image7.png](https://import.viva64.com/docx/blog/1110_UnrealEngine_project_configuration/image7.png)

In the _\.target\.cs_ file, you can set up general settings of _UnrealBuildTool_ which will be executed only during analysis\. To do this, add the following statement:

```cpp
if (StaticAnalyzer == StaticAnalyzer.PVSStudio)
{
  // Settings
}
```

This scenario can be used, for example, to disable the _unity build_ functionality\. 

_Unity build_ is a method used in C and C\+\+ software development to speed up the compilation of projects by combining multiple translation units into a single one\. The feature is enabled by default in _UnrealBuildTool_\.

Since the PVS\-Studio C\+\+ analyzer checks the translation units, you can see in the analysis log that not the source files but merged files with the _Module_ prefix are analyzed\.

This setting allows you to optimize build time\. However, it may have an impact on the analysis\. Large files may cause memory shortages during the analysis\.

To disable the feature, use the following code:

```cpp
if (StaticAnalyzer == StaticAnalyzer.PVSStudio)
{
  bUseUnityBuild = false;
}
```

Voila\! We've finally set up the analysis\.

## New diagnostic rules

The PVS\-Studio team works on more diagnostic rules specific to Unreal Engine\.

For example, the [V1100](https://pvs-studio.com/en/docs/warnings/v1100/) diagnostic rule\. This warning is issued when the analyzer detects a non\-static class data member that has been declared as a pointer to a type derived from _UObject_ inside a class/structure that is not derived from the _UObject_ type\. The Unreal Engine garbage collector may destroy an object addressed by this pointer\.

Another example is the [V1102](https://pvs-studio.com/en/docs/warnings/v1102/) diagnostic rule\. The analyzer issues this warning when it detects a declaration that does not comply with [Naming Conventions](https://docs.unrealengine.com/5.3/en-US/epic-cplusplus-coding-standard-for-unreal-engine/#namingconventions) for Unreal Engine projects\. For example, names of classes inherited from _UObject_ are prefixed by _U_, and enumerations are prefixed by _E_\. Compliance with these conventions is required for the correct operation of the Unreal Header Tool\. 

## Conclusion

As you can see, setting up the PVS\-Studio integration with Unreal Engine is quite straightforward\. Future releases of Unreal Engine and PVS\-Studio will offer new ways for configuring the analysis\. You're welcome to [follow](https://pvs-studio.com/en/subscribe/) our updates\. 

If you want to work both with PVS\-Studio and Unreal Engine, you can [download the analyzer](https://pvs-studio.com/en/pvs-studio/try-free/) and [learn more](https://pvs-studio.com/en/docs/manual/0043/) about the analysis of the Unreal Engine projects\.