[clang][parser] Fix namespace dropping after malformed declarations
authorAlejandro Álvarez Ayllón <alejandro.alvarez@sonarsource.com>
Mon, 15 May 2023 11:39:58 +0000 (07:39 -0400)
committerAaron Ballman <aaron@aaronballman.com>
Mon, 15 May 2023 11:39:58 +0000 (07:39 -0400)
commitb321738f71259d138c9b2002944eb65f099ec2a6
tree3fb967d72b11641b7a3473edf92ed09f31a37007
parent45b899b92f5762c15d435e58666f5eee18e73b40
[clang][parser] Fix namespace dropping after malformed declarations

* After a malformed top-level declaration
* After a malformed templated class method declaration

In both cases, when there is a malformed declaration, any following
namespace is dropped from the AST. This can trigger a cascade of
confusing diagnostics that may hide the original error. An example:
```
// Start #include "SomeFile.h"
template <class T>
void Foo<T>::Bar(void* aRawPtr) {
    (void)(aRawPtr);
}
// End #include "SomeFile.h"

int main() {}
```
We get the original error, plus 19 others from the standard library.
With this patch, we only get the original error.

clangd can also benefit from this patch, as namespaces following the
malformed declaration is now preserved. i.e.
```

MACRO_FROM_MISSING_INCLUDE("X")

namespace my_namespace {
    //...
}
```
Before this patch, my_namespace is not visible for auto-completion.

Differential Revision: https://reviews.llvm.org/D150258
clang/docs/ReleaseNotes.rst
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/ParseTemplate.cpp
clang/test/Parser/cxx-namespace-after-missing-semicolon.cpp [new file with mode: 0644]
clang/test/Parser/cxx-template-recovery.cpp [new file with mode: 0644]