1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmConditionEvaluator.h"
#include <array>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iterator>
#include <list>
#include <sstream>
#include <utility>
#include <cm/string_view>
#include <cmext/algorithm>
#include "cmsys/RegularExpression.hxx"
#include "cmExpandedCommandArgument.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmState.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmValue.h"
#include "cmake.h"
namespace {
auto const keyAND = "AND"_s;
auto const keyCOMMAND = "COMMAND"_s;
auto const keyDEFINED = "DEFINED"_s;
auto const keyEQUAL = "EQUAL"_s;
auto const keyEXISTS = "EXISTS"_s;
auto const keyGREATER = "GREATER"_s;
auto const keyGREATER_EQUAL = "GREATER_EQUAL"_s;
auto const keyIN_LIST = "IN_LIST"_s;
auto const keyIS_ABSOLUTE = "IS_ABSOLUTE"_s;
auto const keyIS_DIRECTORY = "IS_DIRECTORY"_s;
auto const keyIS_NEWER_THAN = "IS_NEWER_THAN"_s;
auto const keyIS_SYMLINK = "IS_SYMLINK"_s;
auto const keyLESS = "LESS"_s;
auto const keyLESS_EQUAL = "LESS_EQUAL"_s;
auto const keyMATCHES = "MATCHES"_s;
auto const keyNOT = "NOT"_s;
auto const keyOR = "OR"_s;
auto const keyParenL = "("_s;
auto const keyParenR = ")"_s;
auto const keyPOLICY = "POLICY"_s;
auto const keySTREQUAL = "STREQUAL"_s;
auto const keySTRGREATER = "STRGREATER"_s;
auto const keySTRGREATER_EQUAL = "STRGREATER_EQUAL"_s;
auto const keySTRLESS = "STRLESS"_s;
auto const keySTRLESS_EQUAL = "STRLESS_EQUAL"_s;
auto const keyTARGET = "TARGET"_s;
auto const keyTEST = "TEST"_s;
auto const keyVERSION_EQUAL = "VERSION_EQUAL"_s;
auto const keyVERSION_GREATER = "VERSION_GREATER"_s;
auto const keyVERSION_GREATER_EQUAL = "VERSION_GREATER_EQUAL"_s;
auto const keyVERSION_LESS = "VERSION_LESS"_s;
auto const keyVERSION_LESS_EQUAL = "VERSION_LESS_EQUAL"_s;
cmSystemTools::CompareOp const MATCH2CMPOP[5] = {
cmSystemTools::OP_LESS, cmSystemTools::OP_LESS_EQUAL,
cmSystemTools::OP_GREATER, cmSystemTools::OP_GREATER_EQUAL,
cmSystemTools::OP_EQUAL
};
// Run-Time to Compile-Time template selector
template <template <typename> class Comp, template <typename> class... Ops>
struct cmRt2CtSelector
{
template <typename T>
static bool eval(int r, T lhs, T rhs)
{
switch (r) {
case 0:
return false;
case 1:
return Comp<T>()(lhs, rhs);
default:
return cmRt2CtSelector<Ops...>::eval(r - 1, lhs, rhs);
}
}
};
template <template <typename> class Comp>
struct cmRt2CtSelector<Comp>
{
template <typename T>
static bool eval(int r, T lhs, T rhs)
{
return r == 1 && Comp<T>()(lhs, rhs);
}
};
std::string bool2string(bool const value)
{
return std::string(std::size_t(1), static_cast<char>('0' + int(value)));
}
bool looksLikeSpecialVariable(const std::string& var,
cm::static_string_view prefix,
const std::size_t varNameLen)
{
// NOTE Expecting a variable name at least 1 char length:
// <prefix> + `{` + <varname> + `}`
return ((prefix.size() + 3) <= varNameLen) &&
cmHasPrefix(var, cmStrCat(prefix, '{')) && var[varNameLen - 1] == '}';
}
} // anonymous namespace
#if defined(__SUNPRO_CC)
# define CM_INHERIT_CTOR(Class, Base, Tpl) \
template <typename... Args> \
Class(Args&&... args) \
: Base Tpl(std::forward<Args>(args)...) \
{ \
}
#else
# define CM_INHERIT_CTOR(Class, Base, Tpl) using Base Tpl ::Base;
#endif
// BEGIN cmConditionEvaluator::cmArgumentList
class cmConditionEvaluator::cmArgumentList
: public std::list<cmExpandedCommandArgument>
{
using base_t = std::list<cmExpandedCommandArgument>;
public:
CM_INHERIT_CTOR(cmArgumentList, list, <cmExpandedCommandArgument>);
class CurrentAndNextIter
{
friend class cmConditionEvaluator::cmArgumentList;
public:
base_t::iterator current;
base_t::iterator next;
CurrentAndNextIter advance(base_t& args)
{
this->current = std::next(this->current);
this->next =
std::next(this->current, difference_type(this->current != args.end()));
return *this;
}
private:
CurrentAndNextIter(base_t& args)
: current(args.begin())
, next(std::next(this->current,
difference_type(this->current != args.end())))
{
}
};
class CurrentAndTwoMoreIter
{
friend class cmConditionEvaluator::cmArgumentList;
public:
base_t::iterator current;
base_t::iterator next;
base_t::iterator nextnext;
CurrentAndTwoMoreIter advance(base_t& args)
{
this->current = std::next(this->current);
this->next =
std::next(this->current, difference_type(this->current != args.end()));
this->nextnext =
std::next(this->next, difference_type(this->next != args.end()));
return *this;
}
private:
CurrentAndTwoMoreIter(base_t& args)
: current(args.begin())
, next(std::next(this->current,
difference_type(this->current != args.end())))
, nextnext(
std::next(this->next, difference_type(this->next != args.end())))
{
}
};
CurrentAndNextIter make2ArgsIterator() { return *this; }
CurrentAndTwoMoreIter make3ArgsIterator() { return *this; }
template <typename Iter>
void ReduceOneArg(const bool value, Iter args)
{
*args.current = cmExpandedCommandArgument(bool2string(value), true);
this->erase(args.next);
}
void ReduceTwoArgs(const bool value, CurrentAndTwoMoreIter args)
{
*args.current = cmExpandedCommandArgument(bool2string(value), true);
this->erase(args.nextnext);
this->erase(args.next);
}
};
// END cmConditionEvaluator::cmArgumentList
cmConditionEvaluator::cmConditionEvaluator(cmMakefile& makefile,
cmListFileBacktrace bt)
: Makefile(makefile)
, Backtrace(std::move(bt))
, Policy12Status(makefile.GetPolicyStatus(cmPolicies::CMP0012))
, Policy54Status(makefile.GetPolicyStatus(cmPolicies::CMP0054))
, Policy57Status(makefile.GetPolicyStatus(cmPolicies::CMP0057))
, Policy64Status(makefile.GetPolicyStatus(cmPolicies::CMP0064))
{
}
//=========================================================================
// order of operations,
// 1. ( ) -- parenthetical groups
// 2. IS_DIRECTORY EXISTS COMMAND DEFINED etc predicates
// 3. MATCHES LESS GREATER EQUAL STRLESS STRGREATER STREQUAL etc binary ops
// 4. NOT
// 5. AND OR
//
// There is an issue on whether the arguments should be values of references,
// for example IF (FOO AND BAR) should that compare the strings FOO and BAR
// or should it really do IF (${FOO} AND ${BAR}) Currently IS_DIRECTORY
// EXISTS COMMAND and DEFINED all take values. EQUAL, LESS and GREATER can
// take numeric values or variable names. STRLESS and STRGREATER take
// variable names but if the variable name is not found it will use the name
// directly. AND OR take variables or the values 0 or 1.
bool cmConditionEvaluator::IsTrue(
const std::vector<cmExpandedCommandArgument>& args, std::string& errorString,
MessageType& status)
{
errorString.clear();
// handle empty invocation
if (args.empty()) {
return false;
}
// store the reduced args in this vector
cmArgumentList newArgs(args.begin(), args.end());
// now loop through the arguments and see if we can reduce any of them
// we do this multiple times. Once for each level of precedence
// parens
using handlerFn_t = bool (cmConditionEvaluator::*)(
cmArgumentList&, std::string&, MessageType&);
const std::array<handlerFn_t, 5> handlers = { {
&cmConditionEvaluator::HandleLevel0, // parenthesis
&cmConditionEvaluator::HandleLevel1, // predicates
&cmConditionEvaluator::HandleLevel2, // binary ops
&cmConditionEvaluator::HandleLevel3, // NOT
&cmConditionEvaluator::HandleLevel4 // AND OR
} };
for (auto fn : handlers) {
// Call the reducer 'till there is anything to reduce...
// (i.e., if after an iteration the size becomes smaller)
auto levelResult = true;
for (auto beginSize = newArgs.size();
(levelResult = (this->*fn)(newArgs, errorString, status)) &&
newArgs.size() < beginSize;
beginSize = newArgs.size()) {
}
if (!levelResult) {
// NOTE `errorString` supposed to be set already
return false;
}
}
// now at the end there should only be one argument left
if (newArgs.size() != 1) {
errorString = "Unknown arguments specified";
status = MessageType::FATAL_ERROR;
return false;
}
return this->GetBooleanValueWithAutoDereference(newArgs.front(), errorString,
status, true);
}
//=========================================================================
cmValue cmConditionEvaluator::GetDefinitionIfUnquoted(
cmExpandedCommandArgument const& argument) const
{
if ((this->Policy54Status != cmPolicies::WARN &&
this->Policy54Status != cmPolicies::OLD) &&
argument.WasQuoted()) {
return nullptr;
}
cmValue def = this->Makefile.GetDefinition(argument.GetValue());
if (def && argument.WasQuoted() &&
this->Policy54Status == cmPolicies::WARN) {
if (!this->Makefile.HasCMP0054AlreadyBeenReported(this->Backtrace.Top())) {
std::ostringstream e;
// clang-format off
e << (cmPolicies::GetPolicyWarning(cmPolicies::CMP0054))
<< "\n"
"Quoted variables like \"" << argument.GetValue() << "\" "
"will no longer be dereferenced when the policy is set to NEW. "
"Since the policy is not set the OLD behavior will be used.";
// clang-format on
this->Makefile.GetCMakeInstance()->IssueMessage(
MessageType::AUTHOR_WARNING, e.str(), this->Backtrace);
}
}
return def;
}
//=========================================================================
cmValue cmConditionEvaluator::GetVariableOrString(
const cmExpandedCommandArgument& argument) const
{
cmValue def = this->GetDefinitionIfUnquoted(argument);
if (!def) {
def = cmValue(argument.GetValue());
}
return def;
}
//=========================================================================
bool cmConditionEvaluator::IsKeyword(
cm::static_string_view keyword,
const cmExpandedCommandArgument& argument) const
{
if ((this->Policy54Status != cmPolicies::WARN &&
this->Policy54Status != cmPolicies::OLD) &&
argument.WasQuoted()) {
return false;
}
const auto isKeyword = argument.GetValue() == keyword;
if (isKeyword && argument.WasQuoted() &&
this->Policy54Status == cmPolicies::WARN) {
if (!this->Makefile.HasCMP0054AlreadyBeenReported(this->Backtrace.Top())) {
std::ostringstream e;
// clang-format off
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0054)
<< "\n"
"Quoted keywords like \"" << argument.GetValue() << "\" "
"will no longer be interpreted as keywords "
"when the policy is set to NEW. "
"Since the policy is not set the OLD behavior will be used.";
// clang-format on
this->Makefile.GetCMakeInstance()->IssueMessage(
MessageType::AUTHOR_WARNING, e.str(), this->Backtrace);
}
}
return isKeyword;
}
//=========================================================================
bool cmConditionEvaluator::GetBooleanValue(
cmExpandedCommandArgument& arg) const
{
// Check basic and named constants.
if (cmIsOn(arg.GetValue())) {
return true;
}
if (cmIsOff(arg.GetValue())) {
return false;
}
// Check for numbers.
if (!arg.empty()) {
char* end;
const double d = std::strtod(arg.GetValue().c_str(), &end);
if (*end == '\0') {
// The whole string is a number. Use C conversion to bool.
return static_cast<bool>(d);
}
}
// Check definition.
cmValue def = this->GetDefinitionIfUnquoted(arg);
return !cmIsOff(def);
}
//=========================================================================
// Boolean value behavior from CMake 2.6.4 and below.
bool cmConditionEvaluator::GetBooleanValueOld(
cmExpandedCommandArgument const& arg, bool const one) const
{
if (one) {
// Old IsTrue behavior for single argument.
if (arg == "0") {
return false;
}
if (arg == "1") {
return true;
}
cmValue def = this->GetDefinitionIfUnquoted(arg);
return !cmIsOff(def);
}
// Old GetVariableOrNumber behavior.
cmValue def = this->GetDefinitionIfUnquoted(arg);
if (!def && std::atoi(arg.GetValue().c_str())) {
def = cmValue(arg.GetValue());
}
return !cmIsOff(def);
}
//=========================================================================
// returns the resulting boolean value
bool cmConditionEvaluator::GetBooleanValueWithAutoDereference(
cmExpandedCommandArgument& newArg, std::string& errorString,
MessageType& status, bool const oneArg) const
{
// Use the policy if it is set.
if (this->Policy12Status == cmPolicies::NEW) {
return this->GetBooleanValue(newArg);
}
if (this->Policy12Status == cmPolicies::OLD) {
return this->GetBooleanValueOld(newArg, oneArg);
}
// Check policy only if old and new results differ.
const auto newResult = this->GetBooleanValue(newArg);
const auto oldResult = this->GetBooleanValueOld(newArg, oneArg);
if (newResult != oldResult) {
switch (this->Policy12Status) {
case cmPolicies::WARN:
errorString = "An argument named \"" + newArg.GetValue() +
"\" appears in a conditional statement. " +
cmPolicies::GetPolicyWarning(cmPolicies::CMP0012);
status = MessageType::AUTHOR_WARNING;
CM_FALLTHROUGH;
case cmPolicies::OLD:
return oldResult;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS: {
errorString = "An argument named \"" + newArg.GetValue() +
"\" appears in a conditional statement. " +
cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0012);
status = MessageType::FATAL_ERROR;
}
case cmPolicies::NEW:
break;
}
}
return newResult;
}
template <int N>
inline int cmConditionEvaluator::matchKeysImpl(
const cmExpandedCommandArgument&)
{
// Zero means "not found"
return 0;
}
template <int N, typename T, typename... Keys>
inline int cmConditionEvaluator::matchKeysImpl(
const cmExpandedCommandArgument& arg, T current, Keys... key)
{
if (this->IsKeyword(current, arg)) {
// Stop searching as soon as smth has found
return N;
}
return matchKeysImpl<N + 1>(arg, key...);
}
template <typename... Keys>
inline int cmConditionEvaluator::matchKeys(
const cmExpandedCommandArgument& arg, Keys... key)
{
// Get index of the matched key (1-based)
return matchKeysImpl<1>(arg, key...);
}
//=========================================================================
// level 0 processes parenthetical expressions
bool cmConditionEvaluator::HandleLevel0(cmArgumentList& newArgs,
std::string& errorString,
MessageType& status)
{
for (auto arg = newArgs.begin(); arg != newArgs.end(); ++arg) {
if (this->IsKeyword(keyParenL, *arg)) {
// search for the closing paren for this opening one
auto depth = 1;
auto argClose = std::next(arg);
for (; argClose != newArgs.end() && depth; ++argClose) {
depth += int(this->IsKeyword(keyParenL, *argClose)) -
int(this->IsKeyword(keyParenR, *argClose));
}
if (depth) {
errorString = "mismatched parenthesis in condition";
status = MessageType::FATAL_ERROR;
return false;
}
// store the reduced args in this vector
auto argOpen = std::next(arg);
const std::vector<cmExpandedCommandArgument> subExpr(
argOpen, std::prev(argClose));
// now recursively invoke IsTrue to handle the values inside the
// parenthetical expression
const auto value = this->IsTrue(subExpr, errorString, status);
*arg = cmExpandedCommandArgument(bool2string(value), true);
argOpen = std::next(arg);
// remove the now evaluated parenthetical expression
newArgs.erase(argOpen, argClose);
}
}
return true;
}
//=========================================================================
// level one handles most predicates except for NOT
bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
MessageType&)
{
const auto policy64IsOld = this->Policy64Status == cmPolicies::OLD ||
this->Policy64Status == cmPolicies::WARN;
for (auto args = newArgs.make2ArgsIterator(); args.current != newArgs.end();
args.advance(newArgs)) {
auto policyCheck = [&, this](const cmPolicies::PolicyID id,
const cmPolicies::PolicyStatus status,
const cm::static_string_view kw) {
if (status == cmPolicies::WARN && this->IsKeyword(kw, *args.current)) {
std::ostringstream e;
e << cmPolicies::GetPolicyWarning(id) << "\n"
<< kw
<< " will be interpreted as an operator "
"when the policy is set to NEW. "
"Since the policy is not set the OLD behavior will be used.";
this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
}
};
// NOTE Checking policies for warnings are not require an access to the
// next arg. Check them first!
policyCheck(cmPolicies::CMP0064, this->Policy64Status, keyTEST);
// NOTE Fail fast: All the predicates below require the next arg to be
// valid
if (args.next == newArgs.end()) {
continue;
}
// does a file exist
if (this->IsKeyword(keyEXISTS, *args.current)) {
newArgs.ReduceOneArg(cmSystemTools::FileExists(args.next->GetValue()),
args);
}
// does a directory with this name exist
else if (this->IsKeyword(keyIS_DIRECTORY, *args.current)) {
newArgs.ReduceOneArg(
cmSystemTools::FileIsDirectory(args.next->GetValue()), args);
}
// does a symlink with this name exist
else if (this->IsKeyword(keyIS_SYMLINK, *args.current)) {
newArgs.ReduceOneArg(cmSystemTools::FileIsSymlink(args.next->GetValue()),
args);
}
// is the given path an absolute path ?
else if (this->IsKeyword(keyIS_ABSOLUTE, *args.current)) {
newArgs.ReduceOneArg(
cmSystemTools::FileIsFullPath(args.next->GetValue()), args);
}
// does a command exist
else if (this->IsKeyword(keyCOMMAND, *args.current)) {
newArgs.ReduceOneArg(
bool(this->Makefile.GetState()->GetCommand(args.next->GetValue())),
args);
}
// does a policy exist
else if (this->IsKeyword(keyPOLICY, *args.current)) {
cmPolicies::PolicyID pid;
newArgs.ReduceOneArg(
cmPolicies::GetPolicyID(args.next->GetValue().c_str(), pid), args);
}
// does a target exist
else if (this->IsKeyword(keyTARGET, *args.current)) {
newArgs.ReduceOneArg(
bool(this->Makefile.FindTargetToUse(args.next->GetValue())), args);
}
// is a variable defined
else if (this->IsKeyword(keyDEFINED, *args.current)) {
const auto& var = args.next->GetValue();
const auto varNameLen = var.size();
auto result = false;
if (looksLikeSpecialVariable(var, "ENV"_s, varNameLen)) {
const auto env = args.next->GetValue().substr(4, varNameLen - 5);
result = cmSystemTools::HasEnv(env);
}
else if (looksLikeSpecialVariable(var, "CACHE"_s, varNameLen)) {
const auto cache = args.next->GetValue().substr(6, varNameLen - 7);
result = bool(this->Makefile.GetState()->GetCacheEntryValue(cache));
}
else {
result = this->Makefile.IsDefinitionSet(args.next->GetValue());
}
newArgs.ReduceOneArg(result, args);
}
// does a test exist
else if (this->IsKeyword(keyTEST, *args.current)) {
if (policy64IsOld) {
continue;
}
newArgs.ReduceOneArg(bool(this->Makefile.GetTest(args.next->GetValue())),
args);
}
}
return true;
}
//=========================================================================
// level two handles most binary operations except for AND OR
bool cmConditionEvaluator::HandleLevel2(cmArgumentList& newArgs,
std::string& errorString,
MessageType& status)
{
for (auto args = newArgs.make3ArgsIterator(); args.current != newArgs.end();
args.advance(newArgs)) {
int matchNo;
// NOTE Handle special case `if(... BLAH_BLAH MATCHES)`
// (i.e., w/o regex to match which is possibly result of
// variable expansion to an empty string)
if (args.next != newArgs.end() &&
this->IsKeyword(keyMATCHES, *args.current)) {
newArgs.ReduceOneArg(false, args);
}
// NOTE Fail fast: All the binary ops below require 2 arguments.
else if (args.next == newArgs.end() || args.nextnext == newArgs.end()) {
continue;
}
else if (this->IsKeyword(keyMATCHES, *args.next)) {
cmValue def = this->GetDefinitionIfUnquoted(*args.current);
std::string def_buf;
if (!def) {
def = cmValue(args.current->GetValue());
} else if (cmHasLiteralPrefix(args.current->GetValue(),
"CMAKE_MATCH_")) {
// The string to match is owned by our match result variables.
// Move it to our own buffer before clearing them.
def_buf = *def;
def = cmValue(def_buf);
}
this->Makefile.ClearMatches();
const auto& rex = args.nextnext->GetValue();
cmsys::RegularExpression regEntry;
if (!regEntry.compile(rex)) {
std::ostringstream error;
error << "Regular expression \"" << rex << "\" cannot compile";
errorString = error.str();
status = MessageType::FATAL_ERROR;
return false;
}
const auto match = regEntry.find(*def);
if (match) {
this->Makefile.StoreMatches(regEntry);
}
newArgs.ReduceTwoArgs(match, args);
}
else if ((matchNo =
this->matchKeys(*args.next, keyLESS, keyLESS_EQUAL, keyGREATER,
keyGREATER_EQUAL, keyEQUAL))) {
cmValue ldef = this->GetVariableOrString(*args.current);
cmValue rdef = this->GetVariableOrString(*args.nextnext);
double lhs;
double rhs;
auto parseDoubles = [&]() {
return std::sscanf(ldef->c_str(), "%lg", &lhs) == 1 &&
std::sscanf(rdef->c_str(), "%lg", &rhs) == 1;
};
// clang-format off
const auto result = parseDoubles() &&
cmRt2CtSelector<
std::less, std::less_equal,
std::greater, std::greater_equal,
std::equal_to
>::eval(matchNo, lhs, rhs);
// clang-format on
newArgs.ReduceTwoArgs(result, args);
}
else if ((matchNo = this->matchKeys(*args.next, keySTRLESS,
keySTRLESS_EQUAL, keySTRGREATER,
keySTRGREATER_EQUAL, keySTREQUAL))) {
const cmValue lhs = this->GetVariableOrString(*args.current);
const cmValue rhs = this->GetVariableOrString(*args.nextnext);
const auto val = (*lhs).compare(*rhs);
// clang-format off
const auto result = cmRt2CtSelector<
std::less, std::less_equal,
std::greater, std::greater_equal,
std::equal_to
>::eval(matchNo, val, 0);
// clang-format on
newArgs.ReduceTwoArgs(result, args);
}
else if ((matchNo =
this->matchKeys(*args.next, keyVERSION_LESS,
keyVERSION_LESS_EQUAL, keyVERSION_GREATER,
keyVERSION_GREATER_EQUAL, keyVERSION_EQUAL))) {
const auto op = MATCH2CMPOP[matchNo - 1];
const std::string& lhs = this->GetVariableOrString(*args.current);
const std::string& rhs = this->GetVariableOrString(*args.nextnext);
const auto result = cmSystemTools::VersionCompare(op, lhs, rhs);
newArgs.ReduceTwoArgs(result, args);
}
// is file A newer than file B
else if (this->IsKeyword(keyIS_NEWER_THAN, *args.next)) {
auto fileIsNewer = 0;
cmsys::Status ftcStatus = cmSystemTools::FileTimeCompare(
args.current->GetValue(), args.nextnext->GetValue(), &fileIsNewer);
newArgs.ReduceTwoArgs(
(!ftcStatus || fileIsNewer == 1 || fileIsNewer == 0), args);
}
else if (this->IsKeyword(keyIN_LIST, *args.next)) {
if (this->Policy57Status != cmPolicies::OLD &&
this->Policy57Status != cmPolicies::WARN) {
cmValue lhs = this->GetVariableOrString(*args.current);
cmValue rhs = this->Makefile.GetDefinition(args.nextnext->GetValue());
newArgs.ReduceTwoArgs(
rhs && cm::contains(cmExpandedList(*rhs, true), *lhs), args);
}
else if (this->Policy57Status == cmPolicies::WARN) {
std::ostringstream e;
e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0057)
<< "\n"
"IN_LIST will be interpreted as an operator "
"when the policy is set to NEW. "
"Since the policy is not set the OLD behavior will be used.";
this->Makefile.IssueMessage(MessageType::AUTHOR_WARNING, e.str());
}
}
}
return true;
}
//=========================================================================
// level 3 handles NOT
bool cmConditionEvaluator::HandleLevel3(cmArgumentList& newArgs,
std::string& errorString,
MessageType& status)
{
for (auto args = newArgs.make2ArgsIterator(); args.next != newArgs.end();
args.advance(newArgs)) {
if (this->IsKeyword(keyNOT, *args.current)) {
const auto rhs = this->GetBooleanValueWithAutoDereference(
*args.next, errorString, status);
newArgs.ReduceOneArg(!rhs, args);
}
}
return true;
}
//=========================================================================
// level 4 handles AND OR
bool cmConditionEvaluator::HandleLevel4(cmArgumentList& newArgs,
std::string& errorString,
MessageType& status)
{
for (auto args = newArgs.make3ArgsIterator(); args.nextnext != newArgs.end();
args.advance(newArgs)) {
int matchNo;
if ((matchNo = this->matchKeys(*args.next, keyAND, keyOR))) {
const auto lhs = this->GetBooleanValueWithAutoDereference(
*args.current, errorString, status);
const auto rhs = this->GetBooleanValueWithAutoDereference(
*args.nextnext, errorString, status);
// clang-format off
const auto result =
cmRt2CtSelector<
std::logical_and, std::logical_or
>::eval(matchNo, lhs, rhs);
// clang-format on
newArgs.ReduceTwoArgs(result, args);
}
}
return true;
}
|