Skip to content

Commit

Permalink
cpplint: allow using-directive for a whitelist of namespaces (#67)
Browse files Browse the repository at this point in the history
This will permit the use of std::chrono and other useful new literals in C++14, which are most conveniently brought in via "using namespace"
  • Loading branch information
Sarcasm authored and jacobperron committed Jan 6, 2022
1 parent 1754c5f commit 224bd69
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions ament_cpplint/ament_cpplint/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5322,12 +5322,28 @@ def CheckLanguage(filename, clean_lines, linenum, file_extension,
'Did you mean "memset(%s, 0, %s)"?'
% (match.group(1), match.group(2)))

if Search(r'\busing namespace\b', line):
if Search(r'\bliterals\b', line):
error(filename, linenum, 'build/namespaces_literals', 5,
'Do not use namespace using-directives. '
'Use using-declarations instead.')
else:
# Check for 'using namespace' which pollutes namespaces.
# This is tricky. Although in general 'using namespace' is a Bad Thing,
# an exception is made for certain standard namespaces, like std::*literals
# and std::placeholders, which are intended to be used in this fashion.
# This whitelist may grow over time as needed if/when shiny new libraries
# come along that are well-behaved in a 'using namespace' context.
# For example, 'using namespace std::chrono_literals;' is allowed, but
# 'using namespace foo;' is not allowed.
# Note that headers are not permitted to use this exception.
match = Search(r'\busing namespace\s+((\w|::)+)', line)
if match:
whitelist = [
'std::chrono_literals',
'std::complex_literals',
'std::literals',
'std::literals::chrono_literals',
'std::literals::complex_literals',
'std::literals::string_literals',
'std::placeholders',
'std::string_literals',
]
if IsHeaderExtension(file_extension) or match.group(1) not in whitelist:
error(filename, linenum, 'build/namespaces', 5,
'Do not use namespace using-directives. '
'Use using-declarations instead.')
Expand Down

0 comments on commit 224bd69

Please sign in to comment.