2014-01-31 09:37:27 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_COMPILERPLUGINS_CLANG_COMPAT_HXX
|
|
|
|
#define INCLUDED_COMPILERPLUGINS_CLANG_COMPAT_HXX
|
|
|
|
|
2017-10-23 23:15:53 +02:00
|
|
|
#include <cstddef>
|
2018-05-03 21:13:35 +02:00
|
|
|
#include <utility>
|
2014-02-25 10:15:28 +01:00
|
|
|
|
2014-01-31 09:37:27 +01:00
|
|
|
#include "clang/AST/Decl.h"
|
2014-02-21 23:59:04 +01:00
|
|
|
#include "clang/AST/Expr.h"
|
2017-05-19 23:51:46 +02:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2014-04-02 17:53:43 +02:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2015-03-24 15:26:35 +02:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2017-12-15 14:20:38 +01:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-01-31 09:50:21 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-01-31 09:37:27 +01:00
|
|
|
|
2016-02-26 12:50:16 +01:00
|
|
|
#include "config_clang.h"
|
|
|
|
|
2014-01-31 10:31:30 +01:00
|
|
|
// Compatibility wrapper to abstract over (trivial) changes in the Clang API:
|
2014-01-31 09:37:27 +01:00
|
|
|
namespace compat {
|
|
|
|
|
2017-10-23 23:15:53 +02:00
|
|
|
inline llvm::StringRef take_front(llvm::StringRef ref, std::size_t N = 1) {
|
|
|
|
#if CLANG_VERSION >= 40000
|
|
|
|
return ref.take_front(N);
|
|
|
|
#else
|
|
|
|
auto const size = ref.size();
|
|
|
|
return N >= size ? ref : ref.drop_back(size - N);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-28 15:28:18 +02:00
|
|
|
|
2016-06-24 16:46:55 +02:00
|
|
|
#if CLANG_VERSION >= 30900
|
|
|
|
inline clang::ArrayRef<clang::ParmVarDecl *> parameters(
|
|
|
|
clang::FunctionDecl const & decl)
|
|
|
|
{
|
|
|
|
return decl.parameters();
|
|
|
|
}
|
2017-12-15 14:20:38 +01:00
|
|
|
#else
|
2016-06-24 16:46:55 +02:00
|
|
|
inline clang::FunctionDecl::param_const_range parameters(
|
|
|
|
clang::FunctionDecl const & decl)
|
|
|
|
{
|
|
|
|
return decl.params();
|
|
|
|
}
|
2016-11-25 11:22:14 +01:00
|
|
|
#endif
|
|
|
|
|
2018-05-03 21:13:35 +02:00
|
|
|
inline std::pair<clang::SourceLocation, clang::SourceLocation> getImmediateExpansionRange(
|
|
|
|
clang::SourceManager const & SM, clang::SourceLocation Loc)
|
|
|
|
{
|
|
|
|
#if CLANG_VERSION >= 70000
|
|
|
|
auto const csr = SM.getImmediateExpansionRange(Loc);
|
|
|
|
if (csr.isCharRange()) { /*TODO*/ }
|
|
|
|
return {csr.getBegin(), csr.getEnd()};
|
|
|
|
#else
|
|
|
|
return SM.getImmediateExpansionRange(Loc);
|
|
|
|
#endif
|
|
|
|
}
|
2014-09-23 13:46:24 +02:00
|
|
|
|
2017-12-10 19:52:57 +01:00
|
|
|
inline bool isPointWithin(
|
|
|
|
clang::SourceManager const & SM, clang::SourceLocation Location, clang::SourceLocation Start,
|
|
|
|
clang::SourceLocation End)
|
|
|
|
{
|
|
|
|
#if CLANG_VERSION >= 60000
|
|
|
|
return SM.isPointWithin(Location, Start, End);
|
|
|
|
#else
|
|
|
|
return
|
|
|
|
Location == Start || Location == End
|
|
|
|
|| (SM.isBeforeInTranslationUnit(Start, Location)
|
|
|
|
&& SM.isBeforeInTranslationUnit(Location, End));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-04-21 12:05:48 +02:00
|
|
|
inline bool isMacroArgExpansion(
|
|
|
|
clang::CompilerInstance& compiler, clang::SourceLocation location,
|
|
|
|
clang::SourceLocation * startLocation)
|
|
|
|
{
|
|
|
|
#if CLANG_VERSION >= 30900
|
|
|
|
return compiler.getSourceManager().isMacroArgExpansion(
|
|
|
|
location, startLocation);
|
|
|
|
#else
|
|
|
|
bool b = compiler.getSourceManager().isMacroArgExpansion(location);
|
|
|
|
if (b) {
|
2016-04-21 13:52:23 +02:00
|
|
|
*startLocation = compiler.getSourceManager()
|
2016-04-21 12:05:48 +02:00
|
|
|
.getSLocEntry(compiler.getSourceManager().getFileID(location))
|
|
|
|
.getExpansion().getExpansionLocStart();
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-03 12:34:38 +02:00
|
|
|
inline llvm::StringRef getImmediateMacroNameForDiagnostics(
|
|
|
|
clang::SourceLocation Loc, clang::SourceManager const & SM,
|
|
|
|
clang::LangOptions const &LangOpts)
|
|
|
|
{
|
|
|
|
#if CLANG_VERSION >= 30900
|
|
|
|
return clang::Lexer::getImmediateMacroNameForDiagnostics(Loc, SM, LangOpts);
|
|
|
|
#else
|
|
|
|
using namespace clang;
|
|
|
|
// Verbatim copy from Clang's lib/Lex/Lexer.cpp:
|
|
|
|
|
2017-07-15 10:19:32 +02:00
|
|
|
assert(Loc.isMacroID() && "Only reasonable to call this on macros");
|
|
|
|
// Walk past macro argument expansion.
|
2017-07-03 12:34:38 +02:00
|
|
|
while (SM.isMacroArgExpansion(Loc))
|
|
|
|
Loc = SM.getImmediateExpansionRange(Loc).first;
|
|
|
|
|
|
|
|
// If the macro's spelling has no FileID, then it's actually a token paste
|
|
|
|
// or stringization (or similar) and not a macro at all.
|
|
|
|
if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(Loc))))
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
// Find the spelling location of the start of the non-argument expansion
|
|
|
|
// range. This is where the macro name was spelled in order to begin
|
|
|
|
// expanding this macro.
|
|
|
|
Loc = SM.getSpellingLoc(SM.getImmediateExpansionRange(Loc).first);
|
|
|
|
|
|
|
|
// Dig out the buffer where the macro name was spelled and the extents of
|
|
|
|
// the name so that we can render it into the expansion note.
|
|
|
|
std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
|
|
|
|
unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
|
|
|
|
StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
|
|
|
|
return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:51:46 +02:00
|
|
|
// Work around <http://reviews.llvm.org/D22128>:
|
|
|
|
//
|
|
|
|
// SfxErrorHandler::GetClassString (svtools/source/misc/ehdl.cxx):
|
|
|
|
//
|
|
|
|
// ErrorResource_Impl aEr(aId, (sal_uInt16)lClassId);
|
|
|
|
// if(aEr)
|
|
|
|
// {
|
|
|
|
// rStr = static_cast<ResString>(aEr).GetString();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// expr->dump():
|
|
|
|
// CXXStaticCastExpr 0x2b74e8e657b8 'class ResString' static_cast<class ResString> <ConstructorConversion>
|
|
|
|
// `-CXXBindTemporaryExpr 0x2b74e8e65798 'class ResString' (CXXTemporary 0x2b74e8e65790)
|
|
|
|
// `-CXXConstructExpr 0x2b74e8e65758 'class ResString' 'void (class ResString &&) noexcept(false)' elidable
|
|
|
|
// `-MaterializeTemporaryExpr 0x2b74e8e65740 'class ResString' xvalue
|
|
|
|
// `-CXXBindTemporaryExpr 0x2b74e8e65720 'class ResString' (CXXTemporary 0x2b74e8e65718)
|
|
|
|
// `-ImplicitCastExpr 0x2b74e8e65700 'class ResString' <UserDefinedConversion>
|
|
|
|
// `-CXXMemberCallExpr 0x2b74e8e656d8 'class ResString'
|
|
|
|
// `-MemberExpr 0x2b74e8e656a0 '<bound member function type>' .operator ResString 0x2b74e8dc1f00
|
|
|
|
// `-DeclRefExpr 0x2b74e8e65648 'struct ErrorResource_Impl' lvalue Var 0x2b74e8e653b0 'aEr' 'struct ErrorResource_Impl'
|
|
|
|
// expr->getSubExprAsWritten()->dump():
|
|
|
|
// MaterializeTemporaryExpr 0x2b74e8e65740 'class ResString' xvalue
|
|
|
|
// `-CXXBindTemporaryExpr 0x2b74e8e65720 'class ResString' (CXXTemporary 0x2b74e8e65718)
|
|
|
|
// `-ImplicitCastExpr 0x2b74e8e65700 'class ResString' <UserDefinedConversion>
|
|
|
|
// `-CXXMemberCallExpr 0x2b74e8e656d8 'class ResString'
|
|
|
|
// `-MemberExpr 0x2b74e8e656a0 '<bound member function type>' .operator ResString 0x2b74e8dc1f00
|
|
|
|
// `-DeclRefExpr 0x2b74e8e65648 'struct ErrorResource_Impl' lvalue Var 0x2b74e8e653b0 'aEr' 'struct ErrorResource_Impl'
|
|
|
|
//
|
|
|
|
// Copies code from Clang's lib/AST/Expr.cpp:
|
|
|
|
namespace detail {
|
|
|
|
inline clang::Expr *skipImplicitTemporary(clang::Expr *expr) {
|
|
|
|
// Skip through reference binding to temporary.
|
|
|
|
if (clang::MaterializeTemporaryExpr *Materialize
|
|
|
|
= clang::dyn_cast<clang::MaterializeTemporaryExpr>(expr))
|
|
|
|
expr = Materialize->GetTemporaryExpr();
|
|
|
|
|
|
|
|
// Skip any temporary bindings; they're implicit.
|
|
|
|
if (clang::CXXBindTemporaryExpr *Binder = clang::dyn_cast<clang::CXXBindTemporaryExpr>(expr))
|
|
|
|
expr = Binder->getSubExpr();
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline clang::Expr *getSubExprAsWritten(clang::CastExpr *This) {
|
|
|
|
clang::Expr *SubExpr = nullptr;
|
|
|
|
clang::CastExpr *E = This;
|
|
|
|
do {
|
|
|
|
SubExpr = detail::skipImplicitTemporary(E->getSubExpr());
|
|
|
|
|
|
|
|
// Conversions by constructor and conversion functions have a
|
|
|
|
// subexpression describing the call; strip it off.
|
|
|
|
if (E->getCastKind() == clang::CK_ConstructorConversion)
|
|
|
|
SubExpr =
|
|
|
|
detail::skipImplicitTemporary(clang::cast<clang::CXXConstructExpr>(SubExpr)->getArg(0));
|
|
|
|
else if (E->getCastKind() == clang::CK_UserDefinedConversion) {
|
|
|
|
assert((clang::isa<clang::CXXMemberCallExpr>(SubExpr) ||
|
|
|
|
clang::isa<clang::BlockExpr>(SubExpr)) &&
|
|
|
|
"Unexpected SubExpr for CK_UserDefinedConversion.");
|
|
|
|
if (clang::isa<clang::CXXMemberCallExpr>(SubExpr))
|
|
|
|
SubExpr = clang::cast<clang::CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the subexpression we're left with is an implicit cast, look
|
|
|
|
// through that, too.
|
|
|
|
} while ((E = clang::dyn_cast<clang::ImplicitCastExpr>(SubExpr)));
|
|
|
|
|
|
|
|
return SubExpr;
|
|
|
|
}
|
|
|
|
inline const clang::Expr *getSubExprAsWritten(const clang::CastExpr *This) {
|
|
|
|
return getSubExprAsWritten(const_cast<clang::CastExpr *>(This));
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:37:53 +02:00
|
|
|
}
|
|
|
|
|
2014-01-31 09:37:27 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|