<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55950>55950</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `CompilerInstance.ExecuteAction` throws an assertion fail when a memory buffer is used as an input file and `ShowStats` is true.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          hypervis0r
      </td>
    </tr>
</table>

<pre>
    # Description
`clang::CompilerInstance.ExecuteAction` will throw an assertion fail when a memory buffer is used as a `FrontendInputFile` and `FrontendOptions.ShowStats` is set to true. The assertion in question is a call to `FrontendInputFile.getFile()`. This fails due to the input being a buffer and not a real file. 

# Root cause
Going up the call stack, we find this bit of code in `FrontendAction::EndSourceFile()`, located in `FrontendAction.cpp`.
```cpp
if (CI.getFrontendOpts().ShowStats) {
    llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; // Bad line here
    CI.getPreprocessor().PrintStats();
    CI.getPreprocessor().getIdentifierTable().PrintStats();
    CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
    CI.getSourceManager().PrintStats();
    llvm::errs() << "\n";
}
```

It seems that this block of code here is simply trying to get the file name to print debug information. To fix this issue, I suggest replacing the `getCurrentFile()` call with a call to `getCurrentFileOrBufferName()`, which would be more applicable here.

# Reproduction
Below is a code snippet reproducing the bug.
```cpp
// main.cpp
auto& frontEndOptions = compilerInvocation.getFrontendOpts();

/*
    Root cause here
*/
frontEndOptions.ShowStats = true;

frontEndOptions.Inputs.clear();

const char* source = "int main(void) { return 1; }";

std::string Filename("fakefile.cpp");
std::unique_ptr<llvm::MemoryBuffer> MB(llvm::MemoryBuffer::getMemBuffer(source, Filename));

frontEndOptions.Inputs.push_back(clang::FrontendInputFile(*MB, clang::InputKind(Language::CXX)));

targetOptions.Triple = llvm::sys::getDefaultTargetTriple();
compilerInstance.createDiagnostics(textDiagnosticPrinter.get(), false);

llvm::LLVMContext context;
std::unique_ptr<clang::CodeGenAction> action = std::make_unique<clang::EmitLLVMOnlyAction>(&context);

/*
    Assertion fail here
*/
if (!compilerInstance.ExecuteAction(*action))
{
        std::cout << "failed\n";
        return 1;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVltz6jgM_jXhxbNMTiiUPPDArWeZbbdnDszOeesYRyHeGjtrO6X8-5WVQLh12jlMBoItS5-kT5LXJtuPoqTHZuCElaWXRkfxLIrH0SAWiutN1BvjMzXbUiqwC-081wK683cQlYexoBODmO2kUswX1uwY14w7BzZssZxLxXYF4BrbwtbYPVtXeQ6WSccqBxnK4haqeLBGe9DZQpeVf0BrQS3X2eneMyF03WVhdkvPvQsyqMiBZ94wbyvoslUBJwCkZv9V4Or3YErwgNTcNNndQG06GUZJihJBG54KXjiWVUBWUL8MJ9gapN6gysajAFYbjwsWuGJ5UMiacNbfGOmfBiUER9frte8m6KhKUkvYMMTiNUqmbAeoA3X6AGEtPTM5EyYL1k_RN0mgRM11tjSVFXDmRVCmjOAew33rbFeUZXD2mPr6Cau0InOGuqYLCk-bClcbOMlGkrLoflIfYvhR6m1bAwNrG3EW9ab4oMYk6k_1cjVeLZarxXTJHp5_4uo9bhxk0N60shb0SVZOz6Mwakct4U9vgmsP-LAJz5iSGlgBFlo0tQM_LJTWCHDO2MaBH1Zq33hAIetNvnAKVxcZQpO5BLvi6wPA31f3J_AM7BK4FcVC5-br-uqkP3HNN_Alr76QmTqmDSfuZxfkOOX1wmMFwtYhU7lv6Ip8ez0SNuSB6lRuS7XHOt0H0mMtbULhFkC1wjTfUoGVATjLYF1tkK65sVtOLGUrg4LvtQHpXAWB2Avmqs0GKxyrrlRckGZUiRhv0Sd0DCqznfTFeTs4F3-2EyrrvxHVWSXtCikKtjOVyrADMOxp2G_KUkkROEC-dq-qPqQ7q0TbYCegsFfWHSmEyGlZlkBOkOTBDQzCh3X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