Skip to content

Commit 854bd32

Browse files
committed
Add check for obsolete CMake end commands
Some commands provide optional arguments: - else() - endforeach() - endfunction() - endif() - endmacro() - endwhile() These can be also checked and warned about in the output as they are not recommended to be used neither improve code readability.
1 parent 081df40 commit 854bd32

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

bin/check-cmake.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,52 @@ function checkCMakeSet(Iterator $files): int
380380
return $status;
381381
};
382382

383+
/**
384+
* Check for obsolete usages of:
385+
* else(<condition>)
386+
* endif(<condition>)
387+
* endforeach(<loop-var>)
388+
* endwhile(<condition>)
389+
* endfunction(<name>)
390+
* endmacro(<name>)
391+
*/
392+
function checkCMakeObsoleteEndCommands(Iterator $files): int
393+
{
394+
$status = 0;
395+
396+
$commands = [
397+
'else',
398+
'endif',
399+
'endforeach',
400+
'endwhile',
401+
'endfunction',
402+
'endmacro',
403+
];
404+
405+
foreach ($files as $file) {
406+
$content = getCMakeCode($file);
407+
408+
foreach ($commands as $command) {
409+
preg_match_all(
410+
'/^[ \t]*(' . $command . ')[ \t]*\((.+)\)/mi',
411+
$content,
412+
$matches,
413+
PREG_SET_ORDER,
414+
);
415+
416+
foreach ($matches as $match) {
417+
$foundCommand = $match[1] ?? '';
418+
$foundArgument = $match[2] ?? '';
419+
420+
$status = 1;
421+
output("E: Replace $foundCommand($foundArgument) with $command()\n in $file\n");
422+
}
423+
}
424+
}
425+
426+
return $status;
427+
}
428+
383429
/**
384430
* Find all local Find* modules in the project.
385431
*/
@@ -673,6 +719,9 @@ function checkAll(array $options): int
673719
$newStatus = checkCMakeSet($allCMakeFiles);
674720
$status = (0 === $status) ? $newStatus : $status;
675721

722+
$newStatus = checkCMakeObsoleteEndCommands($allCMakeFiles);
723+
$status = (0 === $status) ? $newStatus : $status;
724+
676725
$findModules = getFindModules($options['path'] . '/cmake/cmake/modules');
677726
$newStatus = checkFindModules($findModules, $allCMakeFiles);
678727
$status = (0 === $status) ? $newStatus : $status;

0 commit comments

Comments
 (0)