Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Use modern C++11 style #865

Merged

Conversation

hjmjohnson
Copy link
Contributor

@hjmjohnson hjmjohnson commented Jan 14, 2019

Run clang-tidy code modernization refactoring tools. The set of scripts used for this repository have been in heavy use on many large repositories that are moving to C++11 support.

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-,modernize-pass-by-value -header-filter=. -fix

The emptiness of a container should be checked using the empty() method
instead of the size() method. It is not guaranteed that size() is a
constant-time function, and it is generally more efficient and also
shows clearer intent to use empty(). Furthermore some containers may
implement the empty() method but not implement the size() method. Using
empty() whenever possible makes it easier to switch to another container
in the future.

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,readability-container-size-empty  -header-filter=.* -fix
@hjmjohnson hjmjohnson changed the title PERF: Allow compiler to choose best way to construct a copy STYLE: Use modern C++11 style Jan 14, 2019
@hjmjohnson hjmjohnson self-assigned this Jan 14, 2019
@hjmjohnson hjmjohnson added the Language Conformance C++11, style, or language warning topics addressed label Jan 14, 2019
src/lib_json/json_writer.cpp Show resolved Hide resolved
src/lib_json/json_writer.cpp Outdated Show resolved Hide resolved
src/lib_json/json_writer.cpp Outdated Show resolved Hide resolved
src/test_lib_json/jsontest.cpp Outdated Show resolved Hide resolved
src/test_lib_json/jsontest.cpp Outdated Show resolved Hide resolved
src/test_lib_json/main.cpp Outdated Show resolved Hide resolved
C++11 Range based for loops can be used in

Used as a more readable equivalent to the traditional for loop operating over a
range of values, such as all elements in a container, in the forward direction..

Range based loopes are more explicit for only computing the
end location once for containers.

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-loop-convert  -header-filter=.* -fix
… expression

This check is responsible for using the auto type specifier for variable
declarations to improve code readability and maintainability.

The auto type specifier will only be introduced in situations where the
variable type matches the type of the initializer expression. In other words
auto should deduce the same type that was originally spelled in the source

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-use-auto -header-filter = .* -fix
With move semantics added to the language and the standard library updated with
move constructors added for many types it is now interesting to take an
argument directly by value, instead of by const-reference, and then copy. This
check allows the compiler to take care of choosing the best way to construct
the copy.

The transformation is usually beneficial when the calling code passes an rvalue
and assumes the move construction is a cheap operation. This short example
illustrates how the construction of the value happens:

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-pass-by-value  -header-filter=.* -fix
Converts a default constructor’s member initializers into the new
default member initializers in C++11. Other member initializers that match the
default member initializer are removed. This can reduce repeated code or allow
use of ‘= default’.

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-use-default-member-init  -header-filter=.* -fix
This check replaces default bodies of special member functions with
= default;. The explicitly defaulted function declarations enable more
opportunities in optimization, because the compiler might treat
explicitly defaulted functions as trivial.

Additionally, the C++11 use of = default more clearly expreses the
intent for the special member functions.

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-use-equals-default  -header-filter=.* -fix
This check replaces undefined special member functions with
= delete;. The explicitly deleted function declarations enable more
opportunities in optimization, because the compiler might treat
explicitly delted functions as noops.

Additionally, the C++11 use of = delete more clearly expreses the
intent for the special member functions.

SRCDIR=/Users/johnsonhj/src/jsoncpp/ #My local SRC
BLDDIR=/Users/johnsonhj/src/jsoncpp/cmake-build-debug/ #My local BLD

cd /Users/johnsonhj/src/jsoncpp/cmake-build-debug/
run-clang-tidy.py -extra-arg=-D__clang__ -checks=-*,modernize-use-equals-delete  -header-filter=.* -fix
Make the index values consistent with size_t.
Copy link
Contributor Author

@hjmjohnson hjmjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed all identified problems.

src/test_lib_json/jsontest.cpp Outdated Show resolved Hide resolved
@hjmjohnson
Copy link
Contributor Author

@BillyDonahue Thank you for your comments. I have addressed all of the items you identified.

I did not fix one item that was a bit out of scope for this set of changes.

If you would please approve this PR, I'd like to get this incorporated.

@hjmjohnson hjmjohnson dismissed BillyDonahue’s stale review January 16, 2019 00:30

All concerns have been addressed.

@hjmjohnson hjmjohnson merged commit 21a4185 into open-source-parsers:master Jan 16, 2019
@BillyDonahue
Copy link
Contributor

Sorry I didn't get back to this sooner. Thanks for the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Language Conformance C++11, style, or language warning topics addressed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants