Differences between revisions 9 and 13 (spanning 4 versions)
Revision 9 as of 2016-05-28 19:28:45
Size: 1557
Editor: SamatJain
Comment:
Revision 13 as of 2016-05-29 04:40:38
Size: 2642
Editor: SamatJain
Comment: Don't constantly evaluate end() in loops
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[http://clang.llvm.org/extra/clang-tidy/index.html|Clang-Tidy — Extra Clang Tools 3.9 documentation]]

Use:

{{{
clang-tidy -checks='*' main.cpp -- -std=c++11
}}}

[[http://clang.llvm.org/docs/ClangFormat.html|ClangFormat]]: auto-formats C++ code

 * [[https://github.com/andrewseidl/githook-clang-format|Git hook running clang-format]]. Don't use this!
Line 22: Line 10:
== Formatting ==

[[http://clang.llvm.org/docs/ClangFormat.html|ClangFormat]]: auto-formats C++ code

 * [[https://github.com/andrewseidl/githook-clang-format|Git hook running clang-format]]. Don't use this!
Line 24: Line 18:
 * [[http://clang.llvm.org/extra/clang-tidy/index.html|Clang-Tidy]]: C/C++ linting tool, à la Google's cpplint.

{{{
clang-tidy -checks='*' *.cpp -- -std=c++11
}}}
Line 28: Line 27:
# Highest severity # Highest severity warnings only (i.e. memory leaks, etc)
Line 39: Line 38:

== Stuff I never remember ==

=== Unnecessary copies w/ `auto` ===

Beware unncessary copies w/ `auto`; default semantics is to make a copy.

{{{#!highlight c++ numbers=off
// Typically there's no reason to copy.
for (const auto &Val : Container) { observe(Val); }
for (auto &Val : Container) { Val.change(); }

// Remove the reference if you really want a new copy.
for (auto Val : Container) { Val.change(); saveSomewhere(Val); }

// Copy pointers, but make it clear that they're pointers.
for (const auto *Ptr : Container) { observe(*Ptr); }
for (auto *Ptr : Container) { Ptr->change(); }
}}}

=== Don't evaluate `end()` in loops ===

{{{#!highlight c++ numbers=off

// BAD
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  // ... use I ...

// GOOD (unless you are mutating BB; use the above loop if you are, and document you are doing so)
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  // ... use I ...
}}}

C++ style guides

Formatting

ClangFormat: auto-formats C++ code

Static analysis tools

  • Clang-Tidy: C/C++ linting tool, à la Google's cpplint.

clang-tidy -checks='*' *.cpp -- -std=c++11

# Highest severity warnings only (i.e. memory leaks, etc)
cppcheck --enable=all *.cpp
# Everything except style-related linting
cppcheck --enable=warning,performance,portability,information,missingInclude \
         --std=c++11 --library=std.cfg --verbose --quiet \
         *.cpp

Overview articles:

Stuff I never remember

Unnecessary copies w/ `auto`

Beware unncessary copies w/ auto; default semantics is to make a copy.

// Typically there's no reason to copy.
for (const auto &Val : Container) { observe(Val); }
for (auto &Val : Container) { Val.change(); }

// Remove the reference if you really want a new copy.
for (auto Val : Container) { Val.change(); saveSomewhere(Val); }

// Copy pointers, but make it clear that they're pointers.
for (const auto *Ptr : Container) { observe(*Ptr); }
for (auto *Ptr : Container) { Ptr->change(); }

Don't evaluate `end()` in loops

// BAD
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
  // ... use I ...

// GOOD (unless you are mutating BB; use the above loop if you are, and document you are doing so)
BasicBlock *BB = ...
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  // ... use I ...

SamatsWiki: CodingStyle/C++ (last edited 2020-09-02 20:49:51 by SamatJain)