ChuanqiXu created this revision. ChuanqiXu added reviewers: erichkeane, tahonermann, aaron.ballman. ChuanqiXu added a project: clang-modules. Herald added a project: All. ChuanqiXu requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This is an alternative to D129748 <https://reviews.llvm.org/D129748>. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D130331 Files: clang/include/clang/Serialization/ASTWriter.h clang/lib/Serialization/ASTReaderDecl.cpp clang/lib/Serialization/ASTWriter.cpp clang/test/Modules/preferred_name.cppm Index: clang/test/Modules/preferred_name.cppm =================================================================== --- /dev/null +++ clang/test/Modules/preferred_name.cppm @@ -0,0 +1,39 @@ +// Tests that the ODR check wouldn't produce false-positive result for preferred_name attribute. +// +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t +// +// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm +// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t %t/Use.cppm -verify -fsyntax-only +// +//--- foo.h +template<class _CharT> +class foo_templ; + +typedef foo_templ<char> foo; + +template<class _CharT> +class +__attribute__((__preferred_name__(foo))) +foo_templ { +public: + foo_templ() {} +}; + +inline foo_templ<char> bar() +{ + return foo_templ<char>(); +} + +//--- A.cppm +module; +#include "foo.h" +export module A; + +//--- Use.cppm +// expected-no-diagnostics +module; +#include "foo.h" +export module Use; +import A; Index: clang/lib/Serialization/ASTWriter.cpp =================================================================== --- clang/lib/Serialization/ASTWriter.cpp +++ clang/lib/Serialization/ASTWriter.cpp @@ -4347,8 +4347,12 @@ void ASTRecordWriter::AddAttr(const Attr *A) { auto &Record = *this; - if (!A) + // FIXME: Clang can't handle the serialization/deserialization of + // preferred_name properly now. See + // https://github.com/llvm/llvm-project/issues/56490 for example. + if (!A || (isa<PreferredNameAttr>(A) && Writer->isWritingNamedModules())) return Record.push_back(0); + Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs Record.AddIdentifierRef(A->getAttrName()); Index: clang/lib/Serialization/ASTReaderDecl.cpp =================================================================== --- clang/lib/Serialization/ASTReaderDecl.cpp +++ clang/lib/Serialization/ASTReaderDecl.cpp @@ -2922,7 +2922,8 @@ /// Reads attributes from the current stream position. void ASTRecordReader::readAttributes(AttrVec &Attrs) { for (unsigned I = 0, E = readInt(); I != E; ++I) - Attrs.push_back(readAttr()); + if (auto *Attr = readAttr()) + Attrs.push_back(Attr); } //===----------------------------------------------------------------------===// Index: clang/include/clang/Serialization/ASTWriter.h =================================================================== --- clang/include/clang/Serialization/ASTWriter.h +++ clang/include/clang/Serialization/ASTWriter.h @@ -703,6 +703,10 @@ bool hasChain() const { return Chain; } ASTReader *getChain() const { return Chain; } + bool isWritingNamedModules() const { + return WritingModule && WritingModule->isModulePurview(); + } + private: // ASTDeserializationListener implementation void ReaderInitialized(ASTReader *Reader) override;
Index: clang/test/Modules/preferred_name.cppm =================================================================== --- /dev/null +++ clang/test/Modules/preferred_name.cppm @@ -0,0 +1,39 @@ +// Tests that the ODR check wouldn't produce false-positive result for preferred_name attribute. +// +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t +// +// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm +// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t %t/Use.cppm -verify -fsyntax-only +// +//--- foo.h +template<class _CharT> +class foo_templ; + +typedef foo_templ<char> foo; + +template<class _CharT> +class +__attribute__((__preferred_name__(foo))) +foo_templ { +public: + foo_templ() {} +}; + +inline foo_templ<char> bar() +{ + return foo_templ<char>(); +} + +//--- A.cppm +module; +#include "foo.h" +export module A; + +//--- Use.cppm +// expected-no-diagnostics +module; +#include "foo.h" +export module Use; +import A; Index: clang/lib/Serialization/ASTWriter.cpp =================================================================== --- clang/lib/Serialization/ASTWriter.cpp +++ clang/lib/Serialization/ASTWriter.cpp @@ -4347,8 +4347,12 @@ void ASTRecordWriter::AddAttr(const Attr *A) { auto &Record = *this; - if (!A) + // FIXME: Clang can't handle the serialization/deserialization of + // preferred_name properly now. See + // https://github.com/llvm/llvm-project/issues/56490 for example. + if (!A || (isa<PreferredNameAttr>(A) && Writer->isWritingNamedModules())) return Record.push_back(0); + Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs Record.AddIdentifierRef(A->getAttrName()); Index: clang/lib/Serialization/ASTReaderDecl.cpp =================================================================== --- clang/lib/Serialization/ASTReaderDecl.cpp +++ clang/lib/Serialization/ASTReaderDecl.cpp @@ -2922,7 +2922,8 @@ /// Reads attributes from the current stream position. void ASTRecordReader::readAttributes(AttrVec &Attrs) { for (unsigned I = 0, E = readInt(); I != E; ++I) - Attrs.push_back(readAttr()); + if (auto *Attr = readAttr()) + Attrs.push_back(Attr); } //===----------------------------------------------------------------------===// Index: clang/include/clang/Serialization/ASTWriter.h =================================================================== --- clang/include/clang/Serialization/ASTWriter.h +++ clang/include/clang/Serialization/ASTWriter.h @@ -703,6 +703,10 @@ bool hasChain() const { return Chain; } ASTReader *getChain() const { return Chain; } + bool isWritingNamedModules() const { + return WritingModule && WritingModule->isModulePurview(); + } + private: // ASTDeserializationListener implementation void ReaderInitialized(ASTReader *Reader) override;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits