Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSourceFile.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <memory>
8
#include <string>
9
#include <vector>
10
11
#include <cm/string_view>
12
13
#include "cmCustomCommand.h"
14
#include "cmListFileCache.h"
15
#include "cmPropertyMap.h"
16
#include "cmSourceFileLocation.h"
17
#include "cmSourceFileLocationKind.h"
18
#include "cmValue.h"
19
20
class cmMakefile;
21
22
/** \class cmSourceFile
23
 * \brief Represent a class loaded from a makefile.
24
 *
25
 * cmSourceFile represents a class loaded from a makefile.
26
 */
27
class cmSourceFile
28
{
29
public:
30
  /**
31
   * Construct with the makefile storing the source and the initial name
32
   * referencing it. If it shall be marked as generated, this source file's
33
   * kind is assumed to be known, regardless of the given value.
34
   */
35
  cmSourceFile(
36
    cmMakefile* mf, std::string const& name, bool generated,
37
    cmSourceFileLocationKind kind = cmSourceFileLocationKind::Ambiguous);
38
39
  /**
40
   * Get the custom command for this source file
41
   */
42
  cmCustomCommand* GetCustomCommand() const;
43
  void SetCustomCommand(std::unique_ptr<cmCustomCommand> cc);
44
45
  enum class SpecialSourceType
46
  {
47
    // Sources for user-provided sources.
48
    User,
49
    // Source files for object files.
50
    Object,
51
    // Sources representing `CMakeLists.txt` files.
52
    CMakeLists,
53
    // Xcode `Info.plist` files for bundle targets.
54
    BundleInfoPlist,
55
    // Xcode source to force a link to occur with an appropriate language.
56
    XcodeForceLinkerSource,
57
    // Xcode `
58
    XcodeXCConfigFile,
59
    // Header set verification files.
60
    HeaderSetVerificationSource,
61
    // PCH files.
62
    PchHeader,
63
    PchSource,
64
    PchPdbReuseSource,
65
    // Unity sources.
66
    UnitySource,
67
    // Qt support sources.
68
    QtWrapCppSource,
69
    QtAutogenSource,
70
  };
71
  void SetSpecialSourceType(SpecialSourceType type);
72
  bool IsPchHeader() const;
73
  bool IsPchSource() const;
74
75
  //! Set/Get a property of this source file
76
  void SetProperty(std::string const& prop, cmValue value);
77
  void RemoveProperty(std::string const& prop)
78
0
  {
79
0
    this->SetProperty(prop, cmValue{ nullptr });
80
0
  }
81
  void SetProperty(std::string const& prop, std::string const& value)
82
0
  {
83
0
    this->SetProperty(prop, cmValue(value));
84
0
  }
85
  void AppendProperty(std::string const& prop, std::string const& value,
86
                      bool asString = false);
87
  //! Might return a nullptr if the property is not set or invalid
88
  cmValue GetProperty(std::string const& prop) const;
89
  //! Always returns a valid pointer
90
  std::string const& GetSafeProperty(std::string const& prop) const;
91
  bool GetPropertyAsBool(std::string const& prop) const;
92
93
  /** Implement getting a property when called from a CMake language
94
      command like get_property or get_source_file_property.  */
95
  cmValue GetPropertyForUser(std::string const& prop);
96
97
  /// Marks this file as generated
98
  /**
99
   * This stores this file's path in the global table for all generated source
100
   * files.
101
   */
102
  void MarkAsGenerated();
103
  enum class CheckScope
104
  {
105
    Global,
106
    GlobalAndLocal
107
  };
108
  /// Determines if this source file is marked as generated.
109
  /**
110
   * This will check if this file's path is stored in the global table of all
111
   * generated source files. If that is not the case and checkScope is set to
112
   * GlobalAndLocal the value of the possibly existing local GENERATED property
113
   * is returned instead.
114
   * @param checkScope Determines if alternatively for backwards-compatibility
115
   * a local GENERATED property should be considered, too.
116
   * @return true if this source file is marked as generated, otherwise false.
117
   */
118
  bool GetIsGenerated(
119
    CheckScope checkScope = CheckScope::GlobalAndLocal) const;
120
121
  std::vector<BT<std::string>> const& GetCompileOptions() const
122
0
  {
123
0
    return this->CompileOptions;
124
0
  }
125
126
  std::vector<BT<std::string>> const& GetCompileDefinitions() const
127
0
  {
128
0
    return this->CompileDefinitions;
129
0
  }
130
131
  std::vector<BT<std::string>> const& GetIncludeDirectories() const
132
0
  {
133
0
    return this->IncludeDirectories;
134
0
  }
135
136
  /**
137
   * Resolves the full path to the file.  Attempts to locate the file on disk
138
   * and finalizes its location.
139
   */
140
  std::string const& ResolveFullPath(std::string* error = nullptr,
141
                                     std::string* cmp0115Warning = nullptr);
142
143
  /**
144
   * The resolved full path to the file.  The returned file name might be empty
145
   * if the path has not yet been resolved.
146
   */
147
  std::string const& GetFullPath() const;
148
149
  /**
150
   * Get the information currently known about the source file
151
   * location without attempting to locate the file as GetFullPath
152
   * would.  See cmSourceFileLocation documentation.
153
   */
154
  cmSourceFileLocation const& GetLocation() const;
155
156
  /**
157
   * Get the file extension of this source file.
158
   */
159
  std::string const& GetExtension() const;
160
161
  /**
162
   * Get the language of the compiler to use for this source file.
163
   */
164
  std::string const& GetOrDetermineLanguage();
165
  std::string GetLanguage() const;
166
167
  /**
168
   * Return the vector that holds the list of dependencies
169
   */
170
0
  std::vector<std::string> const& GetDepends() const { return this->Depends; }
171
0
  void AddDepend(std::string const& d) { this->Depends.push_back(d); }
172
173
  // Get the properties
174
0
  cmPropertyMap const& GetProperties() const { return this->Properties; }
175
  // Set the properties
176
  void SetProperties(cmPropertyMap properties);
177
178
  /**
179
   * Check whether the given source file location could refer to this
180
   * source.
181
   */
182
  bool Matches(cmSourceFileLocation const&);
183
184
  void SetObjectLibrary(std::string const& objlib);
185
  std::string GetObjectLibrary() const;
186
187
private:
188
  cmSourceFileLocation Location;
189
  cmPropertyMap Properties;
190
  std::unique_ptr<cmCustomCommand> CustomCommand;
191
  std::string Extension;
192
  std::string Language;
193
  std::string FullPath;
194
  std::string ObjectLibrary;
195
  std::vector<std::string> Depends;
196
  std::vector<BT<std::string>> CompileOptions;
197
  std::vector<BT<std::string>> CompileDefinitions;
198
  std::vector<BT<std::string>> IncludeDirectories;
199
  bool FindFullPathFailed = false;
200
  bool IsGenerated = false;
201
  SpecialSourceType SpecialSource = SpecialSourceType::User;
202
203
  bool FindFullPath(std::string* error, std::string* cmp0115Warning);
204
  void CheckExtension();
205
  void CheckLanguage(cm::string_view ext);
206
207
  static std::string const propLANGUAGE;
208
  static std::string const propLOCATION;
209
  static std::string const propGENERATED;
210
  static std::string const propCOMPILE_DEFINITIONS;
211
  static std::string const propCOMPILE_OPTIONS;
212
  static std::string const propINCLUDE_DIRECTORIES;
213
};
214
215
// TODO: Factor out into platform information modules.
216
1
#define CM_HEADER_REGEX "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$"
217
218
#define CM_SOURCE_REGEX                                                       \
219
1
  "\\.(C|F|M|c|c\\+\\+|cc|cpp|mpp|cxx|ixx|cppm|ccm|cxxm|c\\+\\+m|cu"          \
220
1
  "|f|f90|for|fpp|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$"
221
222
1
#define CM_PCH_REGEX "cmake_pch(_[^.]+)?\\.(h|hxx)$"
223
224
1
#define CM_RESOURCE_REGEX "\\.(pdf|plist|png|jpeg|jpg|storyboard|xcassets)$"