Differences between revisions 1 and 39 (spanning 38 versions)
Revision 1 as of 2016-05-12 10:02:33
Size: 106
Editor: SamatJain
Comment:
Revision 39 as of 2020-09-02 20:49:51
Size: 5933
Editor: SamatJain
Comment: Iterating over maps
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]] <<TableOfContents>>

See also:

 * [[CheatSheet/Gcc]]
 * [[VisualStudio]]

== C++ style guides ==

 * [[https://google.github.io/styleguide/cppguide.html|Google]]
 * [[http://llvm.org/docs/CodingStandards.html|LLVM]]
 * [[http://www.chromium.org/developers/coding-style|Chrome/Chromium]]
 * [[https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style|Mozilla]]
 * [[http://www.webkit.org/coding/coding-style.html|WebKit]]
 * Qt's [[https://wiki.qt.io/Qt_Coding_Style|Coding Style]] and [[https://wiki.qt.io/Coding_Conventions|Coding Conventions]]
 * [[https://github.com/isocpp/CppCoreGuidelines|isocpp/CppCoreGuidelines]]: The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++, from Bjarne Stroustrup and Herb Sutter. [[http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines|More easily readable version]]

== `const` correctness ==

 * [[http://www.gotw.ca/gotw/006.htm|GotW #6: Const-Correctness]]
 * http://www.parashift.com/c++-faq-lite/const-correctness.html

== 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!

== Static analysis tools ==

 * [[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
}}}
 * [[http://clang-analyzer.llvm.org/|Clang Static Analyzer]]: sets CC and CXX to use alternate compiler to do analysis
 * [[http://cppcheck.sourceforge.net/|Cppcheck]]; [[https://github.com/danmar/cppcheck/|github:danmar/cppcheck]]

{{{#!highlight sh numbers=off
# 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:

 * [[https://arcanis.me/en/2015/10/17/cppcheck-and-clang-format/|Add cppcheck and clang-format for a cmake project]]

=== Thread safety analysis (TSA) ===

Documentation:

 * [[https://insights.sei.cmu.edu/sei_blog/2014/10/thread-safety-analysis-in-c-and-c.html|Thread Safety Analysis in C and C++]]: CMU blog article on TSA
 * [[https://reviews.llvm.org/D14731|Thread safety analysis support for libc++, annotating mutex, lock_guard, etc]]
 * [[https://news.ycombinator.com/item?id=13981267|Hacker News discussion on thread safety analysis in clang]]

Example projects:

 * [[https://github.com/ClangBuiltLinux/thread-safety-analysis|ClangBuiltLinux/thread-safety-analysis]]: Linux kernel w/ TSA annotations
 * Chromium, annotation macros, from [[https://github.com/chromium/chromium/blob/master/base/thread_annotations.h|base/thread_annotations.h]]. Macros are named different, and they use deprecated TSA names.

=== Lifetime safety analysis ===

 * [[https://herbsutter.com/2018/09/20/lifetime-profile-v1-0-posted/|Lifetime profile v1.0 posted]], initial blog post by Herb Sutter, with link to the [[https://github.com/isocpp/CppCoreGuidelines/blob/master/docs/Lifetime.pdf|Lifetime profile v1.0 paper]]
 * [[https://github.com/mgehre/llvm-project|mgehre/llvm-project]]: clang implementation
 * note: gsl = Guidelines Support Library

== Stuff I never remember ==

 * Namespacing enums in pre-C++11: https://stackoverflow.com/questions/482745/namespaces-for-enum-types-best-practices#484304

=== 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(); }
}}}

For iterating over maps:

{{{#!highlight c++ numbers=off
// Nothing changed
for (const auto& [key, value]: some_map) { observ(value); }
for (auto& [key, value]: some_map) { value.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 ...
}}}

Notes:

 * https://twitter.com/michael90187356/status/1245830680291848193: no difference? end() will get cached anyway.

=== Use `const` iterators if not mutating a structure ===

Expanding on the above:

{{{#!highlight c++ numbers=off
BasicBlock *BB = ...
for (BasicBlock::const_iterator I = BB->cbegin(), E = BB->cend(); I != E; ++I)
  // ... use I ...
}}}

== Naming ==

 * "create" and "destroy" for heap-allocated (i.e. pointer) objects
 * "init" and "finish" for stack-allocated objects

== Dynamic loading of DLL/DSOs ==

Example of loading OpenCL dynamically. Uses a Python parser script to create special header file with defines that map both functions and does appropriate type conversion. https://github.com/opencv/opencv/pull/1542/files

[[http://www.boost.org/doc/libs/master/doc/html/boost_dll.html|Boost.DLL]]: Boost library for easily loading DLLs (e.g. for plugins). Cross platform, but requires runtime libraryes boost_system (integrated into C++11?) and boost_filesystem (integrated into C++17).

https://github.com/knusbaum/CPP-Dynamic-Class-Loading: Class for dynamically loading classes (Linux only)

See also:

C++ style guides

`const` correctness

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:

Thread safety analysis (TSA)

Documentation:

Example projects:

Lifetime safety analysis

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(); }

For iterating over maps:

// Nothing changed
for (const auto& [key, value]: some_map) { observ(value); }
for (auto& [key, value]: some_map) { value.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 ...

Notes:

Use `const` iterators if not mutating a structure

Expanding on the above:

BasicBlock *BB = ...
for (BasicBlock::const_iterator I = BB->cbegin(), E = BB->cend(); I != E; ++I)
  // ... use I ...

Naming

  • "create" and "destroy" for heap-allocated (i.e. pointer) objects
  • "init" and "finish" for stack-allocated objects

Dynamic loading of DLL/DSOs

Example of loading OpenCL dynamically. Uses a Python parser script to create special header file with defines that map both functions and does appropriate type conversion. https://github.com/opencv/opencv/pull/1542/files

Boost.DLL: Boost library for easily loading DLLs (e.g. for plugins). Cross platform, but requires runtime libraryes boost_system (integrated into C++11?) and boost_filesystem (integrated into C++17).

https://github.com/knusbaum/CPP-Dynamic-Class-Loading: Class for dynamically loading classes (Linux only)

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