ExampleΒΆ

Given the following linty file:

# The line is too long and exceeds the default 80 character column limit enforced cmake-lint

# This line has trailing whitespace   

# This line has the wrong line endings


function(BAD_FUNCTION_NAME badArgName good_arg_name)
  # Function names should be lower case
endfunction()

#
macro(bad_macro_name badArgName good_arg_name)
  # Macro names should be upper case
endmacro()

if(FOOBAR)
  foreach(loopvar a b c d)
    # cmake-lint: disable=C0103
    foreach(LOOPVAR2 a b c d)
      # pass
    endforeach()
  endforeach()
  foreach(LOOPVAR3 a b c d)
    # pass
  endforeach()
endif()

if(TRUE)
	message("Hello world")
endif()

if(TRUE)
 message("Hello world")
endif()

if(TRUE)
   message("Hello world")
endif()


# expect:
set(gtest_force_shared_crt ON # cmake-lint: disable=C0103
    CACHE BOOL "See googletest/googletest/CMakeLists.txt")

set(VARNAME varvalue CACHE STRING)

cmake_minimum_required_version(VERSION 2.8.11 VERSION 3.16)

# expect:
install(
  DIRECTORY foo bar/
  DESTINATION some/path
  PATTERN "CVS" EXCLUDE
  PATTERN "scripts/*" PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
                                  GROUP_EXECUTE GROUP_READ)


add_custom_command()

set(_form TARGET PRE_BUILD)
add_custom_command(
  ${form}
  COMMAND echo "hello"
  COMMENT "echo hello")

add_custom_command(
  TARGRET PRE_BUILD
  COMMAND echo "hello"
  COMMENT "echo hello")

add_custom_command(OUTPUT foo)

file()

set(_form TOUCH)
file(${form} foo.py)

file(TOUCHE foo.py)

break()

continue()

# foo: docstring
function(foo)
  foreach(arg ${ARGN})
    if(arg STREQUAL "FOO")
      # parse FOO
    elseif(arg STREQUAL "BAR")
      # parse BAR
    elseif(arg MATCHES "BAZ.*")
      # parse BAZ
    endif()
  endforeach()
endfunction()

set(_foo "hello") set(_bar "goodbye")

set(_foo "hello")


set(_bar "goodbye")

# foo: docstring
function(foo "arg-name")
  # pass
endfunction()

# foo: docstring
function(foo arg arg)
  # pass
endfunction()

# foo: docstring
function(foo arg ARG)
  # pass
endfunction()

return()
message("This code is unreachable")

# cmake-lint: disable=E0109,C0321
# foo:docstring, too many arguments
function(foo a0 a1 a2 a3 a4 a5)

  # Too many branches, too many returns
  if(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  elseif(blah) return()
  else() return()
  endif()

  # too many statements
  message(foo) message(foo) message(foo) message(foo) message(foo) message(foo)
  message(foo) message(foo) message(foo) message(foo) message(foo) message(foo)
  message(foo) message(foo) message(foo) message(foo) message(foo) message(foo)
  message(foo) message(foo) message(foo) message(foo) message(foo) message(foo)
  message(foo) message(foo) message(foo) message(foo) message(foo) message(foo)
endfunction()

# cmake-lint: disable=C0111
# cache (global) variables should be upper snake
set(MyGlobalVar CACHE STRING "my var")
# internal variables are treated as private and should be upper snake with an
# underscore prefix
set(MY_INTERNAL_VAR CACHE INTERNAL "my var")
# directory-scope variables should be upper-snake (public) or lower-snake with
# underscore prefix
set(_INVALID_PRIVATE_NAME "foo")
set(invalid_public_name "foo")
function(foo)
  set(INVALID_LOCAL_NAME "foo")
endfunction()

set(CMAKE_Cxx_STANDARD "11")

list(APPEND CMAKE_Cxx_STANDARD "11")

message("Using C++ standard ${CMAKE_Cxx_STANDARD}")

message("$CMAKE_INSTALL_PREFIX}")
message("{CMAKE_INSTALL_PREFIX}")
message("${CMAKE_INSTALL_PREFIX")

# This file is missing a final newline

The output is:

cmakelang/lint/test/expect_lint.cmake
=====================================
cmakelang/lint/test/expect_lint.cmake:01: [C0301] Line too long (92/80)
cmakelang/lint/test/expect_lint.cmake:03: [C0303] Trailing whitespace
cmakelang/lint/test/expect_lint.cmake:05: [C0327] Wrong line ending (windows)
cmakelang/lint/test/expect_lint.cmake:08,00: [C0111] Missing docstring on function or macro declaration
cmakelang/lint/test/expect_lint.cmake:08,00: [C0305] too many newlines between statements
cmakelang/lint/test/expect_lint.cmake:08,09: [C0103] Invalid function name "BAD_FUNCTION_NAME" doesn't match `[0-9a-z_]+`
cmakelang/lint/test/expect_lint.cmake:13,00: [C0112] Empty docstring on function or macro declaration
cmakelang/lint/test/expect_lint.cmake:13,06: [C0103] Invalid macro name "bad_macro_name" doesn't match `[0-9A-Z_]+`
cmakelang/lint/test/expect_lint.cmake:20,12: [C0103] Invalid argument name "LOOPVAR2" doesn't match `[a-z][a-z0-9_]+`
cmakelang/lint/test/expect_lint.cmake:24,10: [C0103] Invalid argument name "LOOPVAR3" doesn't match `[a-z][a-z0-9_]+`
cmakelang/lint/test/expect_lint.cmake:30,00: [C0306] Tab-policy violation. Found tab but should be space
cmakelang/lint/test/expect_lint.cmake:34,01: [C0307] Bad indentation:
 message
  ^----BodyNode: 1:0->IfBlockNode: 33:0->BodyNode: 33:8->StatementNode: 34:1->FunctionNameNode: 34:1

cmakelang/lint/test/expect_lint.cmake:38,03: [C0307] Bad indentation:
   message
  ^----BodyNode: 1:0->IfBlockNode: 37:0->BodyNode: 37:8->StatementNode: 38:3->FunctionNameNode: 38:3

cmakelang/lint/test/expect_lint.cmake:43,04: [C0103] Invalid CACHE variable name "gtest_force_shared_crt" doesn't match `[A-Z][0-9A-Z_]+`
cmakelang/lint/test/expect_lint.cmake:46,27: [E1120] Missing required positional argument
cmakelang/lint/test/expect_lint.cmake:46,33: [E1120] Missing required positional argument
cmakelang/lint/test/expect_lint.cmake:48,46: [E1122] Duplicate keyword argument VERSION
cmakelang/lint/test/expect_lint.cmake:59,00: [C0305] too many newlines between statements
cmakelang/lint/test/expect_lint.cmake:59,19: [E1120] Missing required positional argument
cmakelang/lint/test/expect_lint.cmake:63,02: [C0114] Form descriminator hidden behind variable dereference
cmakelang/lint/test/expect_lint.cmake:68,02: [E1126] Invalid form descriminator
cmakelang/lint/test/expect_lint.cmake:72,19: [E1125] Missing required keyword argument COMMAND
cmakelang/lint/test/expect_lint.cmake:74,05: [E1120] Missing required positional argument
cmakelang/lint/test/expect_lint.cmake:77,05: [C0114] Form descriminator hidden behind variable dereference
cmakelang/lint/test/expect_lint.cmake:79,05: [E1126] Invalid form descriminator
cmakelang/lint/test/expect_lint.cmake:81,00: [E0103] break outside of loop
cmakelang/lint/test/expect_lint.cmake:81,00: [W0101] Unreachable code
cmakelang/lint/test/expect_lint.cmake:83,00: [E0103] continue outside of loop
cmakelang/lint/test/expect_lint.cmake:83,00: [W0101] Unreachable code
cmakelang/lint/test/expect_lint.cmake:87,02: [C0201] Consider replacing custom parser logic with cmake_parse_arguments
cmakelang/lint/test/expect_lint.cmake:98,18: [C0321] Multiple statements on a single line
cmakelang/lint/test/expect_lint.cmake:103,00: [C0305] too many newlines between statements
cmakelang/lint/test/expect_lint.cmake:106,13: [E0109] Invalid argument name "arg-name" in function/macro definition
cmakelang/lint/test/expect_lint.cmake:111,17: [E0108] Duplicate argument name arg in function/macro definition
cmakelang/lint/test/expect_lint.cmake:116,17: [C0202] Argument name ARG differs from existing argument only in case
cmakelang/lint/test/expect_lint.cmake:120,00: [W0101] Unreachable code
cmakelang/lint/test/expect_lint.cmake:125,00: [R0913] Too many named arguments 6/5
cmakelang/lint/test/expect_lint.cmake:126: [R0911] Too many return statements 17/6
cmakelang/lint/test/expect_lint.cmake:126: [R0912] Too many branches 17/12
cmakelang/lint/test/expect_lint.cmake:126: [R0915] Too many statements 65/50
cmakelang/lint/test/expect_lint.cmake:157,04: [C0103] Invalid CACHE variable name "MyGlobalVar" doesn't match `[A-Z][0-9A-Z_]+`
cmakelang/lint/test/expect_lint.cmake:160,04: [C0103] Invalid INTERNAL variable name "MY_INTERNAL_VAR" doesn't match `_[A-Z][0-9A-Z_]+`
cmakelang/lint/test/expect_lint.cmake:163,04: [C0103] Invalid directory variable name "_INVALID_PRIVATE_NAME" doesn't match `[A-Z][0-9A-Z_]+|_[0-9a-z_]+`
cmakelang/lint/test/expect_lint.cmake:164,04: [C0103] Invalid directory variable name "invalid_public_name" doesn't match `[A-Z][0-9A-Z_]+|_[0-9a-z_]+`
cmakelang/lint/test/expect_lint.cmake:166,06: [C0103] Invalid local variable name "INVALID_LOCAL_NAME" doesn't match `[a-z][a-z0-9_]+`
cmakelang/lint/test/expect_lint.cmake:169,04: [W0105] Assignment to variable 'CMAKE_Cxx_STANDARD' which matches a built-in except for case
cmakelang/lint/test/expect_lint.cmake:171,12: [W0105] Assignment to variable 'CMAKE_Cxx_STANDARD' which matches a built-in except for case
cmakelang/lint/test/expect_lint.cmake:173,08: [W0105] Reference to variable 'CMAKE_Cxx_STANDARD' which matches a built-in except for case
cmakelang/lint/test/expect_lint.cmake:175,08: [W0106] String looks like a variable reference missing an open tag '$CMAKE_INSTALL_PREFIX'
cmakelang/lint/test/expect_lint.cmake:176,08: [W0106] String looks like a variable reference missing an open tag '{CMAKE_INSTALL_PREFIX'
cmakelang/lint/test/expect_lint.cmake:177,08: [W0106] String looks like a variable reference missing an open tag '{CMAKE_INSTALL_PREFIX'
cmakelang/lint/test/expect_lint.cmake:179: [C0304] Final newline missing

Summary
=======
files scanned: 1
found lint:
  Convention: 27
       Error: 12
    Refactor: 4
     Warning: 9