llvm-project/clang/test/Parser/cxx-template-recovery.cpp
Alejandro Álvarez Ayllón b321738f71 [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
2023-05-15 07:39:58 -04:00

14 lines
261 B
C++

// RUN: %clang_cc1 -fsyntax-only -verify %s
template <class T>
void Foo<T>::Bar(void* aRawPtr) { // expected-error {{no template named 'Foo'}}
(void)(aRawPtr);
}
namespace baz {
class klass {};
}
int *variable = 0; // ok
const baz::klass object; // ok