Adapt to Clang trunk SourceManager::getImmediateExpansionRange changes

...of <http://llvm.org/viewvc/llvm-project?view=revision&revision=331155>
"PR37189 Fix incorrect end source location and spelling for a split '>>' token",
changing (among others) the return type of getImmediateExpansionRange from a
std::pair of token locations to CharSourceRange (which will typically also
represent token locations, but might also represent char locations).

For now, map the return value of getImmediateExpansionRange back to a std::pair
(as expected by our compilerplugins code in its current form), and mark the
char location case with a TODO (which will need to be addressed if any of our
plugins starts to produce wrong results due to not handling that char location
case).  In the long run, we should instead adapt our code to use the new return
type of getImmediateExpansionRange directly.

Change-Id: Idc2f5dc43830af4798b55bf605976c4ab146c522
Reviewed-on: https://gerrit.libreoffice.org/53817
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
Stephan Bergmann 2018-05-03 21:13:35 +02:00
parent fe18111ac4
commit b72a31b37f
8 changed files with 39 additions and 22 deletions

View File

@ -11,6 +11,7 @@
#define INCLUDED_COMPILERPLUGINS_CLANG_COMPAT_HXX #define INCLUDED_COMPILERPLUGINS_CLANG_COMPAT_HXX
#include <cstddef> #include <cstddef>
#include <utility>
#include "clang/AST/Decl.h" #include "clang/AST/Decl.h"
#include "clang/AST/Expr.h" #include "clang/AST/Expr.h"
@ -49,6 +50,17 @@ inline clang::FunctionDecl::param_const_range parameters(
} }
#endif #endif
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
}
inline bool isPointWithin( inline bool isPointWithin(
clang::SourceManager const & SM, clang::SourceLocation Location, clang::SourceLocation Start, clang::SourceManager const & SM, clang::SourceLocation Location, clang::SourceLocation Start,

View File

@ -179,7 +179,7 @@ bool ConstParams::CheckTraverseFunctionDecl(FunctionDecl * functionDecl)
canonicalDecl->getLocStart(), compiler.getSourceManager(), compiler.getLangOpts()) }; canonicalDecl->getLocStart(), compiler.getSourceManager(), compiler.getLangOpts()) };
if (name.startswith("DECL_LINK") || name.startswith("DECL_STATIC_LINK")) if (name.startswith("DECL_LINK") || name.startswith("DECL_STATIC_LINK"))
return false; return false;
auto loc2 = compiler.getSourceManager().getImmediateExpansionRange(canonicalDecl->getLocStart()).first; auto loc2 = compat::getImmediateExpansionRange(compiler.getSourceManager(), canonicalDecl->getLocStart()).first;
if (compiler.getSourceManager().isMacroBodyExpansion(loc2)) if (compiler.getSourceManager().isMacroBodyExpansion(loc2))
{ {
StringRef name2 { Lexer::getImmediateMacroName( StringRef name2 { Lexer::getImmediateMacroName(

View File

@ -9,6 +9,7 @@
#include "plugin.hxx" #include "plugin.hxx"
#include "check.hxx" #include "check.hxx"
#include "compat.hxx"
/** /**
Check for calls to CPPUNIT_ASSERT when it should be using CPPUNIT_ASSERT_EQUALS Check for calls to CPPUNIT_ASSERT when it should be using CPPUNIT_ASSERT_EQUALS
@ -95,7 +96,7 @@ bool CppunitAssertEquals::VisitCallExpr(const CallExpr* callExpr)
<< callExpr->getSourceRange(); << callExpr->getSourceRange();
return true; return true;
} }
auto range = compiler.getSourceManager().getImmediateExpansionRange(loc); auto range = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc);
checkExpr( checkExpr(
SourceRange(range.first, range.second), name, SourceRange(range.first, range.second), name,
e2->IgnoreParenImpCasts(), false); e2->IgnoreParenImpCasts(), false);

View File

@ -12,6 +12,8 @@
#include <limits> #include <limits>
#include <set> #include <set>
#include <string> #include <string>
#include "compat.hxx"
#include "plugin.hxx" #include "plugin.hxx"
// //
@ -351,7 +353,7 @@ bool CStyleCast::isLastTokenOfImmediateMacroBodyExpansion(
assert(MI != nullptr); assert(MI != nullptr);
if (spell == MI->getDefinitionEndLoc()) { if (spell == MI->getDefinitionEndLoc()) {
if (macroEnd != nullptr) { if (macroEnd != nullptr) {
*macroEnd = compiler.getSourceManager().getImmediateExpansionRange(loc).second; *macroEnd = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc).second;
} }
return true; return true;
} }
@ -391,8 +393,8 @@ bool CStyleCast::rewriteArithmeticCast(CStyleCastExpr const * expr, char const *
auto secondBegin = expr->getRParenLoc(); auto secondBegin = expr->getRParenLoc();
while (compiler.getSourceManager().isMacroArgExpansion(firstBegin) while (compiler.getSourceManager().isMacroArgExpansion(firstBegin)
&& compiler.getSourceManager().isMacroArgExpansion(secondBegin) && compiler.getSourceManager().isMacroArgExpansion(secondBegin)
&& (compiler.getSourceManager().getImmediateExpansionRange(firstBegin) && (compat::getImmediateExpansionRange(compiler.getSourceManager(), firstBegin)
== compiler.getSourceManager().getImmediateExpansionRange(secondBegin))) == compat::getImmediateExpansionRange(compiler.getSourceManager(), secondBegin)))
{ {
firstBegin = compiler.getSourceManager().getImmediateSpellingLoc(firstBegin); firstBegin = compiler.getSourceManager().getImmediateSpellingLoc(firstBegin);
secondBegin = compiler.getSourceManager().getImmediateSpellingLoc(secondBegin); secondBegin = compiler.getSourceManager().getImmediateSpellingLoc(secondBegin);
@ -424,14 +426,14 @@ bool CStyleCast::rewriteArithmeticCast(CStyleCastExpr const * expr, char const *
// FOO((y)) // FOO((y))
while (compiler.getSourceManager().isMacroArgExpansion(third) while (compiler.getSourceManager().isMacroArgExpansion(third)
&& compiler.getSourceManager().isMacroArgExpansion(fourth) && compiler.getSourceManager().isMacroArgExpansion(fourth)
&& (compiler.getSourceManager().getImmediateExpansionRange(third) && (compat::getImmediateExpansionRange(compiler.getSourceManager(), third)
== compiler.getSourceManager().getImmediateExpansionRange(fourth)) == compat::getImmediateExpansionRange(compiler.getSourceManager(), fourth))
&& compiler.getSourceManager().isAtStartOfImmediateMacroExpansion(third)) && compiler.getSourceManager().isAtStartOfImmediateMacroExpansion(third))
//TODO: check fourth is at end of immediate macro expansion, but //TODO: check fourth is at end of immediate macro expansion, but
// SourceManager::isAtEndOfImmediateMacroExpansion requires a location pointing at the // SourceManager::isAtEndOfImmediateMacroExpansion requires a location pointing at the
// character end of the last token // character end of the last token
{ {
auto const range = compiler.getSourceManager().getImmediateExpansionRange(third); auto const range = compat::getImmediateExpansionRange(compiler.getSourceManager(), third);
third = range.first; third = range.first;
fourth = range.second; fourth = range.second;
macro = true; macro = true;
@ -439,8 +441,8 @@ bool CStyleCast::rewriteArithmeticCast(CStyleCastExpr const * expr, char const *
} }
while (compiler.getSourceManager().isMacroArgExpansion(third) while (compiler.getSourceManager().isMacroArgExpansion(third)
&& compiler.getSourceManager().isMacroArgExpansion(fourth) && compiler.getSourceManager().isMacroArgExpansion(fourth)
&& (compiler.getSourceManager().getImmediateExpansionRange(third) && (compat::getImmediateExpansionRange(compiler.getSourceManager(), third)
== compiler.getSourceManager().getImmediateExpansionRange(fourth))) == compat::getImmediateExpansionRange(compiler.getSourceManager(), fourth)))
{ {
third = compiler.getSourceManager().getImmediateSpellingLoc(third); third = compiler.getSourceManager().getImmediateSpellingLoc(third);
fourth = compiler.getSourceManager().getImmediateSpellingLoc(fourth); fourth = compiler.getSourceManager().getImmediateSpellingLoc(fourth);
@ -474,7 +476,8 @@ bool CStyleCast::rewriteArithmeticCast(CStyleCastExpr const * expr, char const *
} }
break; break;
} }
auto const range = compiler.getSourceManager().getImmediateExpansionRange(third); auto const range = compat::getImmediateExpansionRange(
compiler.getSourceManager(), third);
third = range.first; third = range.first;
fourth = range.second; fourth = range.second;
assert(third.isValid()); assert(third.isValid());
@ -511,7 +514,8 @@ bool CStyleCast::rewriteArithmeticCast(CStyleCastExpr const * expr, char const *
{ {
break; break;
} }
auto const range = compiler.getSourceManager().getImmediateExpansionRange(third); auto const range = compat::getImmediateExpansionRange(
compiler.getSourceManager(), third);
third = range.first; third = range.first;
fourth = range.second; fourth = range.second;
} }

View File

@ -12,6 +12,7 @@
#include "clang/Lex/Lexer.h" #include "clang/Lex/Lexer.h"
#include "compat.hxx"
#include "plugin.hxx" #include "plugin.hxx"
namespace { namespace {
@ -125,8 +126,7 @@ void LiteralToBoolConversion::handleImplicitCastSubExpr(
StringRef name { Lexer::getImmediateMacroName( StringRef name { Lexer::getImmediateMacroName(
loc, compiler.getSourceManager(), compiler.getLangOpts()) }; loc, compiler.getSourceManager(), compiler.getLangOpts()) };
if (name == "sal_False" || name == "sal_True") { if (name == "sal_False" || name == "sal_True") {
loc = compiler.getSourceManager().getImmediateExpansionRange( loc = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc).first;
loc).first;
} }
if (isSharedCAndCppCode(loc)) { if (isSharedCAndCppCode(loc)) {
return; return;

View File

@ -13,6 +13,7 @@
#include <set> #include <set>
#include "check.hxx" #include "check.hxx"
#include "compat.hxx"
#include "plugin.hxx" #include "plugin.hxx"
namespace { namespace {
@ -317,8 +318,7 @@ void Nullptr::handleNull(
// ellipsis, cast to void* // ellipsis, cast to void*
return; return;
} }
loc = compiler.getSourceManager() loc = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc).first;
.getImmediateExpansionRange(loc).first;
if (ignoreLocation( if (ignoreLocation(
compiler.getSourceManager().getSpellingLoc(loc))) compiler.getSourceManager().getSpellingLoc(loc)))
{ {
@ -381,8 +381,8 @@ void Nullptr::rewriteOrWarn(
compiler.getLangOpts()) compiler.getLangOpts())
== "NULL")) == "NULL"))
{ {
locStart = compiler.getSourceManager().getImmediateExpansionRange( locStart = compat::getImmediateExpansionRange(compiler.getSourceManager(), locStart)
locStart).first; .first;
} }
SourceLocation locEnd(expr->getLocEnd()); SourceLocation locEnd(expr->getLocEnd());
while (compiler.getSourceManager().isMacroArgExpansion(locEnd)) { while (compiler.getSourceManager().isMacroArgExpansion(locEnd)) {
@ -396,8 +396,7 @@ void Nullptr::rewriteOrWarn(
compiler.getLangOpts()) compiler.getLangOpts())
== "NULL")) == "NULL"))
{ {
locEnd = compiler.getSourceManager().getImmediateExpansionRange( locEnd = compat::getImmediateExpansionRange(compiler.getSourceManager(), locEnd).first;
locEnd).first;
} }
if (replaceText(SourceRange(compiler.getSourceManager().getSpellingLoc(locStart), compiler.getSourceManager().getSpellingLoc(locEnd)), replacement)) { if (replaceText(SourceRange(compiler.getSourceManager().getSpellingLoc(locStart), compiler.getSourceManager().getSpellingLoc(locEnd)), replacement)) {
return; return;

View File

@ -428,7 +428,7 @@ bool SalCall::isSalCallFunction(FunctionDecl const* functionDecl, SourceLocation
//TODO: If the macro is a function-like macro with a parameter named "SAL_CALL", uses of //TODO: If the macro is a function-like macro with a parameter named "SAL_CALL", uses of
// that parameter in the remainder of the replacement text will be false positives. // that parameter in the remainder of the replacement text will be false positives.
assert(SM.isMacroBodyExpansion(startLoc)); assert(SM.isMacroBodyExpansion(startLoc));
auto const startLoc2 = SM.getImmediateExpansionRange(startLoc).second; auto const startLoc2 = compat::getImmediateExpansionRange(SM, startLoc).second;
auto name = Lexer::getImmediateMacroName(startLoc, SM, compiler.getLangOpts()); auto name = Lexer::getImmediateMacroName(startLoc, SM, compiler.getLangOpts());
while (name.startswith("\\\n")) while (name.startswith("\\\n"))
{ {

View File

@ -11,6 +11,7 @@
#include "sallogareas.hxx" #include "sallogareas.hxx"
#include "check.hxx" #include "check.hxx"
#include "compat.hxx"
#include <clang/Lex/Lexer.h> #include <clang/Lex/Lexer.h>
@ -102,7 +103,7 @@ bool SalLogAreas::VisitCallExpr( const CallExpr* call )
const SourceManager& source = compiler.getSourceManager(); const SourceManager& source = compiler.getSourceManager();
for( SourceLocation loc = call->getLocStart(); for( SourceLocation loc = call->getLocStart();
loc.isMacroID(); loc.isMacroID();
loc = source.getImmediateExpansionRange( loc ).first ) loc = compat::getImmediateExpansionRange(source, loc ).first )
{ {
StringRef inMacro = Lexer::getImmediateMacroName( loc, source, compiler.getLangOpts()); StringRef inMacro = Lexer::getImmediateMacroName( loc, source, compiler.getLangOpts());
if( inMacro == "SAL_DEBUG" || inMacro == "SAL_DEBUG_BACKTRACE" ) if( inMacro == "SAL_DEBUG" || inMacro == "SAL_DEBUG_BACKTRACE" )