Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmRST.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 "cmRST.h"
4
5
#include <algorithm>
6
#include <cstddef>
7
#include <iterator>
8
#include <utility>
9
10
#include "cmsys/FStream.hxx"
11
12
#include "cmAlgorithms.h"
13
#include "cmRange.h"
14
#include "cmStringAlgorithms.h"
15
#include "cmSystemTools.h"
16
#include "cmVersion.h"
17
18
cmRST::cmRST(std::ostream& os, std::string docroot)
19
0
  : OS(os)
20
0
  , DocRoot(std::move(docroot))
21
0
  , CMakeDirective("^.. (cmake:)?("
22
0
                   "command|envvar|genex|signature|variable"
23
0
                   ")::")
24
0
  , CMakeModuleDirective("^.. cmake-module::[ \t]+([^ \t\n]+)$")
25
0
  , ParsedLiteralDirective("^.. parsed-literal::[ \t]*(.*)$")
26
0
  , CodeBlockDirective("^.. code-block::[ \t]*(.*)$")
27
0
  , ReplaceDirective("^.. (\\|[^|]+\\|) replace::[ \t]*(.*)$")
28
0
  , IncludeDirective("^.. include::[ \t]+([^ \t\n]+)$")
29
0
  , TocTreeDirective("^.. toctree::[ \t]*(.*)$")
30
0
  , ProductionListDirective("^.. productionlist::[ \t]*(.*)$")
31
0
  , NoteDirective("^.. note::[ \t]*(.*)$")
32
0
  , VersionDirective("^.. version(added|changed)::[ \t]*(.*)$")
33
0
  , ModuleRST(R"(^#\[(=*)\[\.rst:$)")
34
0
  , CMakeRole("(:cmake)?:("
35
0
              "cref|"
36
0
              "command|cpack_gen|generator|genex|"
37
0
              "variable|envvar|module|policy|"
38
0
              "prop_cache|prop_dir|prop_gbl|prop_inst|prop_sf|"
39
0
              "prop_test|prop_tgt|"
40
0
              "manual"
41
0
              "):`(<*([^`<]|[^` \t]<)*)([ \t]+<[^`]*>)?`")
42
0
  , InlineLink("`(<*([^`<]|[^` \t]<)*)([ \t]+<[^`]*>)?`_")
43
0
  , InlineLiteral("``([^`]*)``")
44
0
  , Substitution("(^|[^A-Za-z0-9_])"
45
0
                 "((\\|[^| \t\r\n]([^|\r\n]*[^| \t\r\n])?\\|)(__|_|))"
46
0
                 "([^A-Za-z0-9_]|$)")
47
0
  , TocTreeLink("^.*[ \t]+<([^>]+)>$")
48
0
{
49
0
  this->Replace["|release|"] = cmVersion::GetCMakeVersion();
50
0
}
51
52
bool cmRST::ProcessFile(std::string const& fname, bool isModule)
53
0
{
54
0
  cmsys::ifstream fin(fname.c_str());
55
0
  if (fin) {
56
0
    this->DocDir = cmSystemTools::GetFilenamePath(fname);
57
0
    if (isModule) {
58
0
      this->ProcessModule(fin);
59
0
    } else {
60
0
      this->ProcessRST(fin);
61
0
    }
62
0
    this->OutputLinePending = true;
63
0
    return true;
64
0
  }
65
0
  return false;
66
0
}
67
68
void cmRST::ProcessRST(std::istream& is)
69
0
{
70
0
  std::string line;
71
0
  while (cmSystemTools::GetLineFromStream(is, line)) {
72
0
    this->ProcessLine(line);
73
0
  }
74
0
  this->Reset();
75
0
}
76
77
void cmRST::ProcessModule(std::istream& is)
78
0
{
79
0
  std::string line;
80
0
  std::string rst;
81
0
  while (cmSystemTools::GetLineFromStream(is, line)) {
82
0
    if (!rst.empty() && rst != "#") {
83
      // Bracket mode: check for end bracket
84
0
      std::string::size_type pos = line.find(rst);
85
0
      if (pos == std::string::npos) {
86
0
        this->ProcessLine(line);
87
0
      } else {
88
0
        if (line[0] != '#') {
89
0
          line.resize(pos);
90
0
          this->ProcessLine(line);
91
0
        }
92
0
        rst.clear();
93
0
        this->Reset();
94
0
        this->OutputLinePending = true;
95
0
      }
96
0
    } else {
97
      // Line mode: check for .rst start (bracket or line)
98
0
      if (rst == "#") {
99
0
        if (line == "#") {
100
0
          this->ProcessLine("");
101
0
          continue;
102
0
        }
103
0
        if (cmHasLiteralPrefix(line, "# ")) {
104
0
          line.erase(0, 2);
105
0
          this->ProcessLine(line);
106
0
          continue;
107
0
        }
108
0
        rst.clear();
109
0
        this->Reset();
110
0
        this->OutputLinePending = true;
111
0
      }
112
0
      if (line == "#.rst:") {
113
0
        rst = "#";
114
0
      } else if (this->ModuleRST.find(line)) {
115
0
        rst = "]" + this->ModuleRST.match(1) + "]";
116
0
      }
117
0
    }
118
0
  }
119
0
  if (rst == "#") {
120
0
    this->Reset();
121
0
  }
122
0
}
123
124
void cmRST::Reset()
125
0
{
126
0
  if (!this->MarkupLines.empty()) {
127
0
    cmRST::UnindentLines(this->MarkupLines);
128
0
  }
129
0
  switch (this->DirectiveType) {
130
0
    case Directive::None:
131
0
      break;
132
0
    case Directive::ParsedLiteral:
133
0
      this->ProcessDirectiveParsedLiteral();
134
0
      break;
135
0
    case Directive::LiteralBlock:
136
0
      this->ProcessDirectiveLiteralBlock();
137
0
      break;
138
0
    case Directive::CodeBlock:
139
0
      this->ProcessDirectiveCodeBlock();
140
0
      break;
141
0
    case Directive::Replace:
142
0
      this->ProcessDirectiveReplace();
143
0
      break;
144
0
    case Directive::TocTree:
145
0
      this->ProcessDirectiveTocTree();
146
0
      break;
147
0
  }
148
0
  this->MarkupType = Markup::None;
149
0
  this->DirectiveType = Directive::None;
150
0
  this->MarkupLines.clear();
151
0
}
152
153
void cmRST::ProcessLine(std::string const& line)
154
0
{
155
0
  bool lastLineEndedInColonColon = this->LastLineEndedInColonColon;
156
0
  this->LastLineEndedInColonColon = false;
157
158
  // A line starting in .. is an explicit markup start.
159
0
  if (line == ".." ||
160
0
      (line.size() >= 3 && line[0] == '.' && line[1] == '.' &&
161
0
       cmIsSpace(line[2]))) {
162
0
    this->Reset();
163
0
    this->MarkupType =
164
0
      (line.find_first_not_of(" \t", 2) == std::string::npos ? Markup::Empty
165
0
                                                             : Markup::Normal);
166
    // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
167
    // NOLINTNEXTLINE(bugprone-branch-clone)
168
0
    if (this->CMakeDirective.find(line)) {
169
      // Output cmake domain directives and their content normally.
170
0
      this->NormalLine(line);
171
0
    } else if (this->CMakeModuleDirective.find(line)) {
172
      // Process cmake-module directive: scan .cmake file comments.
173
0
      std::string file = this->CMakeModuleDirective.match(1);
174
0
      if (file.empty() || !this->ProcessInclude(file, Include::Module)) {
175
0
        this->NormalLine(line);
176
0
      }
177
0
    } else if (this->ParsedLiteralDirective.find(line)) {
178
      // Record the literal lines to output after whole block.
179
0
      this->DirectiveType = Directive::ParsedLiteral;
180
0
      this->MarkupLines.push_back(this->ParsedLiteralDirective.match(1));
181
0
    } else if (this->CodeBlockDirective.find(line)) {
182
      // Record the literal lines to output after whole block.
183
      // Ignore the language spec and record the opening line as blank.
184
0
      this->DirectiveType = Directive::CodeBlock;
185
0
      this->MarkupLines.emplace_back();
186
0
    } else if (this->ReplaceDirective.find(line)) {
187
      // Record the replace directive content.
188
0
      this->DirectiveType = Directive::Replace;
189
0
      this->ReplaceName = this->ReplaceDirective.match(1);
190
0
      this->MarkupLines.push_back(this->ReplaceDirective.match(2));
191
0
    } else if (this->IncludeDirective.find(line)) {
192
      // Process the include directive or output the directive and its
193
      // content normally if it fails.
194
0
      std::string file = this->IncludeDirective.match(1);
195
0
      if (file.empty() || !this->ProcessInclude(file, Include::Normal)) {
196
0
        this->NormalLine(line);
197
0
      }
198
0
    } else if (this->TocTreeDirective.find(line)) {
199
      // Record the toctree entries to process after whole block.
200
0
      this->DirectiveType = Directive::TocTree;
201
0
      this->MarkupLines.push_back(this->TocTreeDirective.match(1));
202
0
    } else if (this->ProductionListDirective.find(line)) {
203
      // Output productionlist directives and their content normally.
204
0
      this->NormalLine(line);
205
0
    } else if (this->NoteDirective.find(line)) {
206
      // Output note directives and their content normally.
207
0
      this->NormalLine(line);
208
0
    } else if (this->VersionDirective.find(line)) {
209
      // Output versionadded and versionchanged directives and their content
210
      // normally.
211
0
      this->NormalLine(line);
212
0
    }
213
0
  }
214
  // An explicit markup start followed by nothing but whitespace and a
215
  // blank line does not consume any indented text following.
216
0
  else if (this->MarkupType == Markup::Empty && line.empty()) {
217
0
    this->NormalLine(line);
218
0
  }
219
  // Indented lines following an explicit markup start are explicit markup.
220
0
  else if (this->MarkupType != Markup::None &&
221
0
           (line.empty() || cmIsSpace(line[0]))) {
222
0
    this->MarkupType = Markup::Normal;
223
    // Record markup lines if the start line was recorded.
224
0
    if (!this->MarkupLines.empty()) {
225
0
      this->MarkupLines.push_back(line);
226
0
    }
227
0
  }
228
  // A blank line following a paragraph ending in "::" starts a literal block.
229
0
  else if (lastLineEndedInColonColon && line.empty()) {
230
    // Record the literal lines to output after whole block.
231
0
    this->MarkupType = Markup::Normal;
232
0
    this->DirectiveType = Directive::LiteralBlock;
233
0
    this->MarkupLines.emplace_back();
234
0
    this->OutputLine("", false);
235
0
  }
236
  // Print non-markup lines.
237
0
  else {
238
0
    this->NormalLine(line);
239
0
    this->LastLineEndedInColonColon =
240
0
      (line.size() >= 2 && line[line.size() - 2] == ':' && line.back() == ':');
241
0
  }
242
0
}
243
244
void cmRST::NormalLine(std::string const& line)
245
0
{
246
0
  this->Reset();
247
0
  this->OutputLine(line, true);
248
0
}
249
250
void cmRST::OutputLine(std::string const& line_in, bool inlineMarkup)
251
0
{
252
0
  if (this->OutputLinePending) {
253
0
    this->OS << "\n";
254
0
    this->OutputLinePending = false;
255
0
  }
256
0
  if (inlineMarkup) {
257
0
    std::string line = this->ReplaceSubstitutions(line_in);
258
0
    std::string::size_type pos = 0;
259
0
    for (;;) {
260
0
      std::string::size_type* first = nullptr;
261
0
      std::string::size_type role_start = std::string::npos;
262
0
      std::string::size_type link_start = std::string::npos;
263
0
      std::string::size_type lit_start = std::string::npos;
264
0
      if (this->CMakeRole.find(line.c_str() + pos)) {
265
0
        role_start = this->CMakeRole.start();
266
0
        first = &role_start;
267
0
      }
268
0
      if (this->InlineLiteral.find(line.c_str() + pos)) {
269
0
        lit_start = this->InlineLiteral.start();
270
0
        if (!first || lit_start < *first) {
271
0
          first = &lit_start;
272
0
        }
273
0
      }
274
0
      if (this->InlineLink.find(line.c_str() + pos)) {
275
0
        link_start = this->InlineLink.start();
276
0
        if (!first || link_start < *first) {
277
0
          first = &link_start;
278
0
        }
279
0
      }
280
0
      if (first == &role_start) {
281
0
        this->OS << line.substr(pos, role_start);
282
0
        std::string text = this->CMakeRole.match(3);
283
        // If a command reference has no explicit target and
284
        // no explicit "(...)" then add "()" to the text.
285
0
        if (this->CMakeRole.match(2) == "command" &&
286
0
            this->CMakeRole.match(5).empty() &&
287
0
            text.find_first_of("()") == std::string::npos) {
288
0
          text += "()";
289
0
        }
290
0
        this->OS << "``" << text << "``";
291
0
        pos += this->CMakeRole.end();
292
0
      } else if (first == &lit_start) {
293
0
        this->OS << line.substr(pos, lit_start);
294
0
        std::string text = this->InlineLiteral.match(1);
295
0
        pos += this->InlineLiteral.end();
296
0
        this->OS << "``" << text << "``";
297
0
      } else if (first == &link_start) {
298
0
        this->OS << line.substr(pos, link_start);
299
0
        std::string text = this->InlineLink.match(1);
300
0
        bool escaped = false;
301
0
        for (char c : text) {
302
0
          if (escaped) {
303
0
            escaped = false;
304
0
            this->OS << c;
305
0
          } else if (c == '\\') {
306
0
            escaped = true;
307
0
          } else {
308
0
            this->OS << c;
309
0
          }
310
0
        }
311
0
        pos += this->InlineLink.end();
312
0
      } else {
313
0
        break;
314
0
      }
315
0
    }
316
0
    this->OS << line.substr(pos) << "\n";
317
0
  } else {
318
0
    this->OS << line_in << "\n";
319
0
  }
320
0
}
321
322
std::string cmRST::ReplaceSubstitutions(std::string const& line)
323
0
{
324
0
  std::string out;
325
0
  std::string::size_type pos = 0;
326
0
  while (this->Substitution.find(line.c_str() + pos)) {
327
0
    std::string::size_type start = this->Substitution.start(2);
328
0
    std::string::size_type end = this->Substitution.end(2);
329
0
    std::string substitute = this->Substitution.match(3);
330
0
    auto replace = this->Replace.find(substitute);
331
0
    if (replace != this->Replace.end()) {
332
0
      std::pair<std::set<std::string>::iterator, bool> replaced =
333
0
        this->Replaced.insert(substitute);
334
0
      if (replaced.second) {
335
0
        substitute = this->ReplaceSubstitutions(replace->second);
336
0
        this->Replaced.erase(replaced.first);
337
0
      }
338
0
    }
339
0
    out += line.substr(pos, start);
340
0
    out += substitute;
341
0
    pos += end;
342
0
  }
343
0
  out += line.substr(pos);
344
0
  return out;
345
0
}
346
347
void cmRST::OutputMarkupLines(bool inlineMarkup)
348
0
{
349
0
  for (auto line : this->MarkupLines) {
350
0
    if (!line.empty()) {
351
0
      line = cmStrCat(' ', line);
352
0
    }
353
0
    this->OutputLine(line, inlineMarkup);
354
0
  }
355
0
  this->OutputLinePending = true;
356
0
}
357
358
bool cmRST::ProcessInclude(std::string file, Include type)
359
0
{
360
0
  bool found = false;
361
0
  if (this->IncludeDepth < 10) {
362
0
    cmRST r(this->OS, this->DocRoot);
363
0
    r.IncludeDepth = this->IncludeDepth + 1;
364
0
    r.OutputLinePending = this->OutputLinePending;
365
0
    if (type != Include::TocTree) {
366
0
      r.Replace = this->Replace;
367
0
    }
368
0
    if (file[0] == '/') {
369
0
      file = this->DocRoot + file;
370
0
    } else {
371
0
      file = this->DocDir + "/" + file;
372
0
    }
373
0
    found = r.ProcessFile(file, type == Include::Module);
374
0
    if (type != Include::TocTree) {
375
0
      this->Replace = r.Replace;
376
0
    }
377
0
    this->OutputLinePending = r.OutputLinePending;
378
0
  }
379
0
  return found;
380
0
}
381
382
void cmRST::ProcessDirectiveParsedLiteral()
383
0
{
384
0
  this->OutputMarkupLines(true);
385
0
}
386
387
void cmRST::ProcessDirectiveLiteralBlock()
388
0
{
389
0
  this->OutputMarkupLines(false);
390
0
}
391
392
void cmRST::ProcessDirectiveCodeBlock()
393
0
{
394
0
  this->OutputMarkupLines(false);
395
0
}
396
397
void cmRST::ProcessDirectiveReplace()
398
0
{
399
  // Record markup lines as replacement text.
400
0
  std::string& replacement = this->Replace[this->ReplaceName];
401
0
  replacement += cmJoin(this->MarkupLines, " ");
402
0
  this->ReplaceName.clear();
403
0
}
404
405
void cmRST::ProcessDirectiveTocTree()
406
0
{
407
  // Process documents referenced by toctree directive.
408
0
  for (std::string const& line : this->MarkupLines) {
409
0
    if (!line.empty() && line[0] != ':') {
410
0
      if (this->TocTreeLink.find(line)) {
411
0
        std::string const& link = this->TocTreeLink.match(1);
412
0
        this->ProcessInclude(link + ".rst", Include::TocTree);
413
0
      } else {
414
0
        this->ProcessInclude(line + ".rst", Include::TocTree);
415
0
      }
416
0
    }
417
0
  }
418
0
}
419
420
void cmRST::UnindentLines(std::vector<std::string>& lines)
421
0
{
422
  // Remove the common indentation from the second and later lines.
423
0
  std::string indentText;
424
0
  std::string::size_type indentEnd = 0;
425
0
  bool first = true;
426
0
  for (size_t i = 1; i < lines.size(); ++i) {
427
0
    std::string const& line = lines[i];
428
429
    // Do not consider empty lines.
430
0
    if (line.empty()) {
431
0
      continue;
432
0
    }
433
434
    // Record indentation on first non-empty line.
435
0
    if (first) {
436
0
      first = false;
437
0
      indentEnd = line.find_first_not_of(" \t");
438
0
      indentText = line.substr(0, indentEnd);
439
0
      continue;
440
0
    }
441
442
    // Truncate indentation to match that on this line.
443
0
    indentEnd = std::min(indentEnd, line.size());
444
0
    for (std::string::size_type j = 0; j != indentEnd; ++j) {
445
0
      if (line[j] != indentText[j]) {
446
0
        indentEnd = j;
447
0
        break;
448
0
      }
449
0
    }
450
0
  }
451
452
  // Update second and later lines.
453
0
  for (size_t i = 1; i < lines.size(); ++i) {
454
0
    std::string& line = lines[i];
455
0
    if (!line.empty()) {
456
0
      line = line.substr(indentEnd);
457
0
    }
458
0
  }
459
460
0
  auto it = lines.cbegin();
461
0
  size_t leadingEmpty = std::distance(it, cmFindNot(lines, std::string()));
462
463
0
  auto rit = lines.crbegin();
464
0
  size_t trailingEmpty =
465
0
    std::distance(rit, cmFindNot(cmReverseRange(lines), std::string()));
466
467
0
  if ((leadingEmpty + trailingEmpty) >= lines.size()) {
468
    // All lines are empty.  The markup block is empty.  Leave only one.
469
0
    lines.resize(1);
470
0
    return;
471
0
  }
472
473
0
  auto contentEnd = cmRotate(lines.begin(), lines.begin() + leadingEmpty,
474
0
                             lines.end() - trailingEmpty);
475
0
  lines.erase(contentEnd, lines.end());
476
0
}