Skip to content

Commit 4849c7f

Browse files
committed
clang-wrapper.cc: introduce helpers to reduce code duplication
Change-Id: I1544bc18922dddf91879d59c991ff9d36d16935a TN: V916-015
1 parent 2568411 commit 4849c7f

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

tools/gnatcov/clang-wrapper.cc

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ using namespace clang::cxloc;
4646
using namespace clang::cxstring;
4747
using namespace clang::cxtu;
4848

49+
/* Return the AST context corresponding to the given translation unit TU. */
50+
51+
static ASTContext &
52+
getContext (CXTranslationUnit TU)
53+
{
54+
return cxtu::getASTUnit (TU)->getASTContext ();
55+
}
56+
57+
/* Likewise, but starting from a cursor. */
58+
59+
static ASTContext &
60+
getContext (CXCursor C)
61+
{
62+
return getContext (getCursorTU (C));
63+
}
64+
65+
/* Return the SourceManager corresponding to the given translation unit TU. */
66+
67+
static const SourceManager &
68+
getSourceManager (CXTranslationUnit TU)
69+
{
70+
return cxtu::getASTUnit (TU)->getSourceManager ();
71+
}
72+
73+
/* Translate a source location to a cursor source location in the given
74+
translation unit TU. */
75+
76+
static CXSourceLocation
77+
translateSLoc (CXTranslationUnit TU, SourceLocation Loc)
78+
{
79+
return cxloc::translateSourceLocation (getContext (TU), Loc);
80+
}
81+
4982
/* Convert a clang Stmt type to a libclang CXCursor structure. The CXCursor C
5083
is simply used to get a relevant declaration and translation unit to tie
5184
the returned cursor to. */
@@ -368,7 +401,7 @@ clang_getOpcodeStr (CXCursor C)
368401
extern "C" CXSourceLocation
369402
clang_getOperatorLoc (CXCursor C)
370403
{
371-
ASTUnit *CXXUnit = cxtu::getASTUnit (getCursorTU (C));
404+
CXTranslationUnit TU = getCursorTU (C);
372405
SourceLocation sloc;
373406
if (clang_isExpression (C.kind))
374407
if (const Stmt *S = cxcursor::getCursorStmt (C))
@@ -383,7 +416,7 @@ clang_getOperatorLoc (CXCursor C)
383416
default:
384417
return clang_getNullLocation ();
385418
}
386-
return cxloc::translateSourceLocation (CXXUnit->getASTContext (), sloc);
419+
return translateSLoc (TU, sloc);
387420
}
388421

389422
/* If the given expression is a wrapping expression (i.e. a parenthesized
@@ -438,11 +471,9 @@ extern "C" CXCursor
438471
clang_getParent (CXCursor C)
439472
{
440473
assert (clang_isStatement (C.kind) || clang_isExpression (C.kind));
441-
ASTUnit *astUnit = cxtu::getASTUnit (getCursorTU (C));
442-
ASTContext &astContext = astUnit->getASTContext ();
443474
if (const Stmt *S = cxcursor::getCursorStmt (C))
444475
{
445-
const auto Parents = astContext.getParents (*S);
476+
const auto Parents = getContext (C).getParents (*S);
446477
if (Parents.empty ())
447478
return clang_getNullCursor ();
448479
const auto &SParent = Parents[0];
@@ -486,14 +517,12 @@ extern "C" unsigned
486517
clang_isMacroArgExpansion (CXSourceLocation Loc, CXSourceLocation *StartLoc,
487518
CXTranslationUnit TU)
488519
{
489-
const SourceManager &SM = cxtu::getASTUnit (TU)->getSourceManager ();
520+
const SourceManager &SM = getSourceManager (TU);
490521
const SourceLocation SLoc = translateSourceLocation (Loc);
491-
ASTUnit *astUnit = cxtu::getASTUnit (TU);
492-
ASTContext &astContext = astUnit->getASTContext ();
493522
SourceLocation Result;
494523
if (SM.isMacroArgExpansion (SLoc, &Result))
495524
{
496-
*StartLoc = cxloc::translateSourceLocation (astContext, Result);
525+
*StartLoc = translateSLoc (TU, Result);
497526
return 1;
498527
}
499528
return 0;
@@ -502,48 +531,37 @@ clang_isMacroArgExpansion (CXSourceLocation Loc, CXSourceLocation *StartLoc,
502531
extern "C" CXSourceLocation
503532
clang_getImmediateMacroCallerLoc (CXSourceLocation Loc, CXTranslationUnit TU)
504533
{
505-
SourceManager &SM = cxtu::getASTUnit (TU)->getSourceManager ();
506-
ASTUnit *astUnit = cxtu::getASTUnit (TU);
507-
ASTContext &astContext = astUnit->getASTContext ();
534+
const SourceManager &SM = getSourceManager (TU);
508535
SourceLocation SLoc = translateSourceLocation (Loc);
509536
if (SLoc.isMacroID ())
510-
return cxloc::translateSourceLocation (
511-
astContext, SM.getImmediateMacroCallerLoc (SLoc));
537+
return translateSLoc (TU, SM.getImmediateMacroCallerLoc (SLoc));
512538
return Loc;
513539
}
514540

515541
extern "C" CXSourceLocation
516542
clang_getImmediateExpansionLoc (CXSourceLocation Loc, CXTranslationUnit TU)
517543
{
518-
SourceManager &SM = cxtu::getASTUnit (TU)->getSourceManager ();
519-
ASTUnit *astUnit = cxtu::getASTUnit (TU);
520-
ASTContext &astContext = astUnit->getASTContext ();
544+
const SourceManager &SM = getSourceManager (TU);
521545
SourceLocation SLoc = translateSourceLocation (Loc);
522-
return cxloc::translateSourceLocation (
523-
astContext, SM.getImmediateExpansionRange (SLoc).getBegin ());
546+
return translateSLoc (TU, SM.getImmediateExpansionRange (SLoc).getBegin ());
524547
}
525548

526549
extern "C" CXString
527550
clang_getImmediateMacroNameForDiagnostics (CXSourceLocation Loc,
528551
CXTranslationUnit TU)
529552
{
530553
SourceLocation SLoc = translateSourceLocation (Loc);
531-
SourceManager &SM = cxtu::getASTUnit (TU)->getSourceManager ();
532-
ASTUnit *astUnit = cxtu::getASTUnit (TU);
533-
ASTContext &astContext = astUnit->getASTContext ();
554+
const SourceManager &SM = getSourceManager (TU);
534555
return createDup (Lexer::getImmediateMacroNameForDiagnostics (
535-
SLoc, SM, astContext.getLangOpts ()));
556+
SLoc, SM, getContext (TU).getLangOpts ()));
536557
}
537558

538559
extern "C" CXSourceLocation
539560
clang_getExpansionEnd (CXTranslationUnit TU, CXSourceLocation Loc)
540561
{
541562
SourceLocation SLoc = translateSourceLocation (Loc);
542-
SourceManager &SM = cxtu::getASTUnit (TU)->getSourceManager ();
543-
ASTUnit *astUnit = cxtu::getASTUnit (TU);
544-
ASTContext &astContext = astUnit->getASTContext ();
545-
return cxloc::translateSourceLocation (
546-
astContext, SM.getExpansionRange (SLoc).getEnd ());
563+
const SourceManager &SM = getSourceManager (TU);
564+
return translateSLoc (TU, SM.getExpansionRange (SLoc).getEnd ());
547565
}
548566

549567
extern "C" CXTranslationUnit
@@ -557,6 +575,6 @@ clang_getCursorTU (CXCursor C)
557575
extern "C" void
558576
clang_printLocation (CXTranslationUnit TU, CXSourceLocation Loc)
559577
{
560-
const SourceManager &SM = cxtu::getASTUnit (TU)->getSourceManager ();
578+
const SourceManager &SM = getSourceManager (TU);
561579
clang::cxloc::translateSourceLocation (Loc).dump (SM);
562580
}

0 commit comments

Comments
 (0)