llvm-project/clang/lib/Parse
Barry Revzin 92310454bf Make LLVM build in C++20 mode
Part of the <=> changes in C++20 make certain patterns of writing equality
operators ambiguous with themselves (sorry!).
This patch goes through and adjusts all the comparison operators such that
they should work in both C++17 and C++20 modes. It also makes two other small
C++20-specific changes (adding a constructor to a type that cases to be an
aggregate, and adding casts from u8 literals which no longer have type
const char*).

There were four categories of errors that this review fixes.
Here are canonical examples of them, ordered from most to least common:

// 1) Missing const
namespace missing_const {
    struct A {
    #ifndef FIXED
        bool operator==(A const&);
    #else
        bool operator==(A const&) const;
    #endif
    };

    bool a = A{} == A{}; // error
}

// 2) Type mismatch on CRTP
namespace crtp_mismatch {
    template <typename Derived>
    struct Base {
    #ifndef FIXED
        bool operator==(Derived const&) const;
    #else
        // in one case changed to taking Base const&
        friend bool operator==(Derived const&, Derived const&);
    #endif
    };

    struct D : Base<D> { };

    bool b = D{} == D{}; // error
}

// 3) iterator/const_iterator with only mixed comparison
namespace iter_const_iter {
    template <bool Const>
    struct iterator {
        using const_iterator = iterator<true>;

        iterator();

        template <bool B, std::enable_if_t<(Const && !B), int> = 0>
        iterator(iterator<B> const&);

    #ifndef FIXED
        bool operator==(const_iterator const&) const;
    #else
        friend bool operator==(iterator const&, iterator const&);
    #endif
    };

    bool c = iterator<false>{} == iterator<false>{} // error
          || iterator<false>{} == iterator<true>{}
          || iterator<true>{} == iterator<false>{}
          || iterator<true>{} == iterator<true>{};
}

// 4) Same-type comparison but only have mixed-type operator
namespace ambiguous_choice {
    enum Color { Red };

    struct C {
        C();
        C(Color);
        operator Color() const;
        bool operator==(Color) const;
        friend bool operator==(C, C);
    };

    bool c = C{} == C{}; // error
    bool d = C{} == Red;
}

Differential revision: https://reviews.llvm.org/D78938
2020-12-17 10:44:10 +00:00
..
CMakeLists.txt [openmp] Base of tablegen generated OpenMP common declaration 2020-06-23 10:32:32 -04:00
ParseAST.cpp [Support] Add TimeTraceScope constructor without detail arg 2019-12-11 14:32:21 +00:00
ParseCXXInlineMethods.cpp [NFC, Refactor] Modernize enum FunctionDefinitionKind (DeclSpech.h) into a scoped enum 2020-11-21 09:49:52 -06:00
ParseDecl.cpp Fix inconsistent availability attribute message string literal check. 2020-12-08 12:33:59 -05:00
ParseDeclCXX.cpp [NFC, Refactor] Modernize enum FunctionDefinitionKind (DeclSpech.h) into a scoped enum 2020-11-21 09:49:52 -06:00
ParseExpr.cpp Print source location in the error message when parens are missing around sizeof typename and the expression is inside macro expansion 2020-12-16 12:03:31 -08:00
ParseExprCXX.cpp P0857R0: Parse a requires-clause after an explicit 2020-12-03 15:54:16 -08:00
ParseInit.cpp [clang][CodeComplete] Support for designated initializers 2020-01-28 16:34:15 +01:00
ParseObjc.cpp [NFC, Refactor] Rename the (scoped) enum DeclaratorContext's enumerators to remove duplication 2020-11-10 23:40:12 -06:00
ParseOpenMP.cpp Make LLVM build in C++20 mode 2020-12-17 10:44:10 +00:00
ParsePragma.cpp Add option 'exceptions' to pragma clang fp 2020-10-31 17:36:12 +07:00
ParseStmt.cpp [NFC, Refactor] Rename the (scoped) enum DeclaratorContext's enumerators to remove duplication 2020-11-10 23:40:12 -06:00
ParseStmtAsm.cpp [Parser] ParseMicrosoftAsmStatement - Replace bit '|' operator with logical '||' operator. (PR47071) 2020-10-05 14:23:28 +01:00
ParseTemplate.cpp PR45699: Fix crash if an unexpanded parameter pack appears in a 2020-12-03 15:26:06 -08:00
ParseTentative.cpp [clang] Add a new nullability annotation for swift async: _Nullable_result 2020-12-07 17:19:20 -05:00
Parser.cpp [NFC, Refactor] Modernize enum FunctionDefinitionKind (DeclSpech.h) into a scoped enum 2020-11-21 09:49:52 -06:00