I have meet a problem when using clang as a JIT compiler frontend for OpenCL, we freak the input file with addRemappedFile and append the user’s clang option with some the OpenCL related options into CompilerInvocation. But we meet an assert bug in a test case that user passed some options like “-invalid- --build-- options” and the assertion is:
…/tools/clang/lib/Basic/SourceManager.cpp:73: void clang::SrcMgr::ContentCache::replaceBuffer(llvm::MemoryBuffer*, bool): Assertion `0 && “Replacing with the same buffer”’ failed.
After using gdb to look into it, I found that the “options” is parsed as another input file but without any file added with addRemappedFile. In release version the clang will report some error like “error: error reading 'options”. It seem in clang::CompilerInstance::ExecuteAction, we will run clang::FrontendAction::BeginSourceFile twice as there are two inputs and the function call : clang::CompilerInstance::createPreprocessor → InitializeFileRemapping → overrideFileContents → clang::SrcMgr::ContentCache::replaceBuffer will be called twice with the same args no matter what the input it is running. Thus for there is only one buffer, it will replace it with itself in the second run and hit the assert.
My problem here is that if the assert in replaceBuffer has any useage and why the createPreprocessor function call does not care which inputs it is running now and the InitializeFileRemapping will run twice for same CompilerInstance if two inputs are there, but the RemappedFile is the same in two run. Or if we should try to not pass this kind of invalid options into clang?