Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmFileSetMetadata.cxx
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
#include "cmFileSetMetadata.h"
4
5
#include <map>
6
#include <string>
7
#include <utility>
8
9
#include <cmext/algorithm>
10
#include <cmext/string_view>
11
12
#include "cmsys/RegularExpression.hxx"
13
14
#include "cmMakefile.h"
15
#include "cmMessageType.h"
16
#include "cmStringAlgorithms.h"
17
#include "cmSystemTools.h"
18
19
namespace cm {
20
namespace FileSetMetadata {
21
cm::string_view VisibilityToName(Visibility vis)
22
0
{
23
0
  switch (vis) {
24
0
    case Visibility::Interface:
25
0
      return "INTERFACE"_s;
26
0
    case Visibility::Public:
27
0
      return "PUBLIC"_s;
28
0
    case Visibility::Private:
29
0
      return "PRIVATE"_s;
30
0
  }
31
0
  return ""_s;
32
0
}
33
34
Visibility VisibilityFromName(cm::string_view name, cmMakefile* mf)
35
0
{
36
0
  if (name == "INTERFACE"_s) {
37
0
    return Visibility::Interface;
38
0
  }
39
0
  if (name == "PUBLIC"_s) {
40
0
    return Visibility::Public;
41
0
  }
42
0
  if (name == "PRIVATE"_s) {
43
0
    return Visibility::Private;
44
0
  }
45
0
  auto msg = cmStrCat("File set visibility \"", name, "\" is not valid.");
46
0
  if (mf) {
47
0
    mf->IssueMessage(MessageType::FATAL_ERROR, msg);
48
0
  } else {
49
0
    cmSystemTools::Error(msg);
50
0
  }
51
0
  return Visibility::Private;
52
0
}
53
54
bool VisibilityIsForSelf(Visibility vis)
55
0
{
56
0
  switch (vis) {
57
0
    case Visibility::Interface:
58
0
      return false;
59
0
    case Visibility::Public:
60
0
    case Visibility::Private:
61
0
      return true;
62
0
  }
63
0
  return false;
64
0
}
65
66
bool VisibilityIsForInterface(Visibility vis)
67
0
{
68
0
  switch (vis) {
69
0
    case Visibility::Interface:
70
0
    case Visibility::Public:
71
0
      return true;
72
0
    case Visibility::Private:
73
0
      return false;
74
0
  }
75
0
  return false;
76
0
}
77
78
cm::string_view const HEADERS = "HEADERS"_s;
79
cm::string_view const SOURCES = "SOURCES"_s;
80
cm::string_view const CXX_MODULES = "CXX_MODULES"_s;
81
82
namespace {
83
std::map<cm::string_view, FileSetDescriptor> const FileSetDescriptors{
84
  { cm::FileSetMetadata::HEADERS,
85
    { cm::FileSetMetadata::HEADERS,
86
      cm::FileSetMetadata::FileSetLookup::Target,
87
      { DependencyMode ::Includables },
88
      DependencyMode ::Includables,
89
      { cm::FileSetMetadata::FileSetAttributes::FilesInMultipleFileSets } } },
90
  { cm::FileSetMetadata::SOURCES,
91
    { cm::FileSetMetadata::SOURCES,
92
      cm::FileSetMetadata::FileSetLookup::Dependencies,
93
      { DependencyMode ::IndependentFiles, DependencyMode ::Includables },
94
      DependencyMode ::Includables,
95
      { cm::FileSetMetadata::FileSetAttributes::FrameworkCompatible,
96
        cm::FileSetMetadata::FileSetAttributes::UnityBuild } } },
97
  { cm::FileSetMetadata::CXX_MODULES,
98
    { cm::FileSetMetadata::CXX_MODULES,
99
      cm::FileSetMetadata::FileSetLookup::Target,
100
      { DependencyMode ::IndependentFiles },
101
      DependencyMode ::IndependentFiles,
102
      {} } },
103
};
104
105
std::vector<cm::string_view> KnownTypes{ HEADERS, SOURCES, CXX_MODULES };
106
107
cmsys::RegularExpression const ValidNameRegex("^[a-z0-9][a-zA-Z0-9_]*$");
108
}
109
110
cm::optional<FileSetDescriptor> GetFileSetDescriptor(cm::string_view type)
111
0
{
112
0
  auto it = FileSetDescriptors.find(type);
113
0
  if (it != FileSetDescriptors.end()) {
114
0
    return it->second;
115
0
  }
116
0
  return cm::nullopt;
117
0
}
118
119
DependencyMode GetDependencyMode(cm::string_view type)
120
0
{
121
0
  auto descriptor = GetFileSetDescriptor(type);
122
0
  if (descriptor) {
123
0
    return descriptor->DefaultDependency;
124
0
  }
125
0
  return DependencyMode::Includables;
126
0
}
127
DependencyMode GetDependencyMode(cm::string_view type,
128
                                 DependencyMode requestedMode)
129
0
{
130
0
  auto descriptor = GetFileSetDescriptor(type);
131
0
  if (descriptor) {
132
    // Select the requested mode or the next-weakest mode that is supported by
133
    // the file set type
134
0
    auto mode = descriptor->SupportedDependencies.lower_bound(requestedMode);
135
0
    return mode == descriptor->SupportedDependencies.end()
136
0
      ? descriptor->DefaultDependency
137
0
      : *mode;
138
0
  }
139
0
  return DependencyMode::Includables;
140
0
}
141
142
AttributeSet GetAttributes(cm::string_view type)
143
0
{
144
0
  auto descriptor = GetFileSetDescriptor(type);
145
0
  if (descriptor) {
146
0
    return descriptor->Attributes;
147
0
  }
148
0
  return {};
149
0
}
150
bool IsFrameworkSupported(cm::string_view type)
151
0
{
152
0
  return GetAttributes(type).contains(FileSetAttributes::FrameworkCompatible);
153
0
}
154
155
std::vector<cm::string_view> const& GetKnownTypes()
156
0
{
157
0
  return KnownTypes;
158
0
}
159
160
bool IsKnownType(cm::string_view type)
161
0
{
162
0
  return cm::contains(GetKnownTypes(), type);
163
0
}
164
165
bool IsValidName(cm::string_view name)
166
0
{
167
0
  cmsys::RegularExpressionMatch match;
168
0
  return ValidNameRegex.find(name.data(), match);
169
0
}
170
}
171
}