Skip to content

Instantly share code, notes, and snippets.

@ffevotte
Created January 6, 2014 14:50
Show Gist options
  • Save ffevotte/8283795 to your computer and use it in GitHub Desktop.
Save ffevotte/8283795 to your computer and use it in GitHub Desktop.
Create a precompiled header with libclang
#include "foo.hxx"
int main () {
return 0;
}
#include "boost/aligned_storage.hpp"
#include "boost/any.hpp"
#include "boost/array.hpp"
#include "boost/asio.hpp"
#include "boost/assert.hpp"
#include "boost/assign.hpp"
#include "boost/atomic.hpp"
#include "boost/bimap.hpp"
#include "boost/bind.hpp"
#include "boost/blank_fwd.hpp"
#include "boost/blank.hpp"
#include "boost/call_traits.hpp"
#include "boost/cast.hpp"
#include "boost/cerrno.hpp"
#include "boost/checked_delete.hpp"
#include "boost/chrono.hpp"
#include "boost/circular_buffer_fwd.hpp"
#include "boost/circular_buffer.hpp"
#include "boost/compressed_pair.hpp"
#include "boost/concept_archetype.hpp"
#include "boost/concept_check.hpp"
#include "boost/config.hpp"
#include "boost/crc.hpp"
#include "boost/cregex.hpp"
#include "boost/cstdint.hpp"
#include "boost/cstdlib.hpp"
#include "boost/current_function.hpp"
#include "boost/date_time.hpp"
#include "boost/dynamic_bitset_fwd.hpp"
#include "boost/dynamic_bitset.hpp"
#include "boost/enable_shared_from_this.hpp"
#include "foo.hxx"
int main () {
return 0;
}
#include <clang-c/Index.h>
#include <sys/time.h>
#include <iostream>
class Timer {
public:
Timer () {
reset();
}
double get () {
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_sec - start_.tv_sec + 1e-6 * (now.tv_usec - start_.tv_usec);
}
void reset () {
gettimeofday (&start_, NULL);
}
private:
struct timeval start_;
};
void displayDiagnostics (CXTranslationUnit TU) {
if (TU == 0) {
std::cerr << "Parsing error!" << std::endl;
return;
}
int numDiagnostics = clang_getNumDiagnostics (TU);
std::cerr << numDiagnostics << " diagnostics" << std::endl;
for (int i=0 ; i<numDiagnostics ; ++i) {
auto diagnostic = clang_getDiagnostic (TU, i);
auto string = clang_formatDiagnostic (diagnostic,
clang_defaultDiagnosticDisplayOptions());
std::cerr << clang_getCString (string) << std::endl;
clang_disposeString (string);
clang_disposeDiagnostic (diagnostic);
}
}
int main () {
auto Idx = clang_createIndex (0, 0);
CXTranslationUnit TU;
Timer t;
{
char const *args[] = { "-xc++", "foo.hxx" };
int nargs = 2;
t.reset();
TU = clang_parseTranslationUnit(Idx, 0, args, nargs, 0, 0, CXTranslationUnit_ForSerialization);
std::cerr << "PCH parse time: " << t.get() << std::endl;
displayDiagnostics (TU);
clang_saveTranslationUnit (TU, "foo.pch", clang_defaultSaveOptions(TU));
clang_disposeTranslationUnit (TU);
}
{
char const *args[] = { "-include-pch", "foo.pch", "foo.cxx" };
int nargs = 3;
t.reset();
TU = clang_createTranslationUnitFromSourceFile(Idx, 0, nargs, args, 0, 0);
std::cerr << "Parsing time: " << t.get() << std::endl;
displayDiagnostics (TU);
clang_disposeTranslationUnit (TU);
}
{
char const *args[] = { "-include-pch", "foo.pch", "foo2.cxx" };
int nargs = 3;
t.reset();
TU = clang_createTranslationUnitFromSourceFile(Idx, 0, nargs, args, 0, 0);
std::cerr << "Parsing time: " << t.get() << std::endl;
displayDiagnostics (TU);
clang_disposeTranslationUnit (TU);
}
return 0;
}
@4ntoine
Copy link

4ntoine commented Aug 11, 2020

That's interesting. I have opposite results for 1e^5 of #define VAR %i header: 0.5s pch parsing and 1.5s source file with pch parsing. I can provide a snippet to look into, if interested (https://stackoverflow.com/questions/63354055/clang-pch-performance-worse-that-with-headers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment