how to find a search term in source code
<p>I'm looking for a way to search for a given term in a project's C/C++ code, while ignoring any occurrences in comments and strings.</p>

<p>As the code base is rather large, i am searching for a way to <em>automatically</em> identify the lines of code matching my search term, as they need manual inspection.</p>

<p>If possible I'd like to perform the search on my linux system.</p>

<h1>background</h1>

<p>the code base in question is a realtime signal processing engine with a large number of 3rd party plugins. plugins are implemented in a variety of languages (mostly C, but also C++ and others; currently I only care for those two), no standards have been enforced.</p>

<p>our code base currently uses the built-in type <code>float</code> for floating-point numbers and we would like to replace that with a <code>typedef</code> that would allow us to use doubles.
we would like to find all occurrences of <code>float</code> in the actual code (ignoring legit uses in comments and printouts).</p>

<p>What complicates things furthermore, is that there are some (albeit few) <em>legit</em> uses of <code>float</code> in the code payload (so we are really looking for a way to identify all places that require manual inspection, rather than run some automatic search-and-replace.)</p>

<p>the code also contains C-style static casts to <code>(float)</code>, so relying on compiler warnings to identify type mismatches is often not an option.</p>

<p>the code base consists of more than 3000 (C and C++) files accumulating about 750000 lines of code.</p>

<p>the code is cross-platform (linux, osx, w32 being the main targets; but also freebsd and similar), and is compiled with the various native compilers (gcc/g++, clang/clang++, VisualStudio,...).</p>

<h1>so far...</h1>

<p>so far I'm using something ugly like:</p>

<pre><code> grep "\bfloat\b" | sed -e 's|//.*||' -e 's|"[^"]*"||g' | grep "\bfloat\b"
</code></pre>

<p>but I'm thinking that there <em>must</em> be some better way to search only payload code.</p>