summaryrefslogtreecommitdiffstats
path: root/src/linguist/lupdate/clangtoolastreader.cpp
blob: 64c8ad431f742040da0e0dab3d48baf2e3247e6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Linguist of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "clangtoolastreader.h"
#include "translator.h"

QT_BEGIN_NAMESPACE

namespace LupdatePrivate
{
    /*
        Retrieves the context for the NOOP macros using the context of
        the NamedDeclaration within which the Macro is.
        The context is stripped of the function or method part as it used not to be retrieved
        in the previous cpp parser.
    */
    QString contextForNoopMacro(clang::NamedDecl *namedDecl)
    {
        QStringList context;
        const clang::DeclContext *decl = namedDecl->getDeclContext();
        while (decl) {
            if (clang::isa<clang::NamedDecl>(decl) && !decl->isFunctionOrMethod()) {
                if (const auto *namespaceDecl = clang::dyn_cast<clang::NamespaceDecl>(decl)) {
                    context.prepend(namespaceDecl->isAnonymousNamespace()
                        ? QStringLiteral("(anonymous namespace)")
                        : QString::fromStdString(namespaceDecl->getDeclName().getAsString()));
                } else if (const auto *recordDecl = clang::dyn_cast<clang::RecordDecl>(decl)) {
                    static const QString anonymous = QStringLiteral("(anonymous %1)");
                    context.prepend(recordDecl->getIdentifier()
                        ? QString::fromStdString(recordDecl->getDeclName().getAsString())
                        : anonymous.arg(QLatin1String(recordDecl->getKindName().data())));
                }
            }
            decl = decl->getParent();
        }
        return context.join(QStringLiteral("::"));
    }

    QString contextForFunctionDecl(clang::FunctionDecl *func, const std::string &funcName)
    {
        std::string context;
#if (LUPDATE_CLANG_VERSION >= LUPDATE_CLANG_VERSION_CHECK(10,0,0))
        {
            llvm::raw_string_ostream tmp(context);
            func->printQualifiedName(tmp);
        }
#else
        context = func->getQualifiedNameAsString();
#endif
        return QString::fromStdString(context.substr(0, context.find("::" + funcName, 0)));
    }

    static bool capture(const QRegularExpression &exp, const QString &line, QString *i, QString *c)
    {
        i->clear(), c->clear();
        auto result = exp.match(line);
        if (!result.hasMatch())
            return false;

        *i = result.captured(QLatin1String("identifier"));
        *c = result.captured(QStringLiteral("comment")).trimmed();

        if (*i == QLatin1String("%"))
            *c = LupdatePrivate::cleanQuote(c->toStdString(), QuoteCompulsary::Left);

        return !c->isEmpty();
    }

    bool hasQuote(llvm::StringRef source)
    {
        return source.contains("\"");
    }

    bool trFunctionPresent(llvm::StringRef text)
    {
        if (text.contains(llvm::StringRef("qtTrId(")))
            return true;
        if (text.contains(llvm::StringRef("tr(")))
            return true;
        if (text.contains(llvm::StringRef("trUtf8(")))
            return true;
        if (text.contains(llvm::StringRef("translate(")))
            return true;
        if (text.contains(llvm::StringRef("Q_DECLARE_TR_FUNCTIONS(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TR_N_NOOP(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRID_N_NOOP(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRANSLATE_N_NOOP(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRANSLATE_N_NOOP3(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TR_NOOP(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRID_NOOP(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRANSLATE_NOOP(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRANSLATE_NOOP3(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TR_NOOP_UTF8(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRANSLATE_NOOP_UTF8(")))
            return true;
        if (text.contains(llvm::StringRef("QT_TRANSLATE_NOOP3_UTF8(")))
            return true;
        return false;
    }

    bool isPointWithin(const clang::SourceRange &sourceRange, const clang::SourceLocation &point,
        const clang::SourceManager &sm)
    {
        clang::SourceLocation start = sourceRange.getBegin();
        clang::SourceLocation end = sourceRange.getEnd();
        return point == start || point == end || (sm.isBeforeInTranslationUnit(start, point)
            && sm.isBeforeInTranslationUnit(point, end));
    }
}

/*
    The visit call expression function is called automatically after the
    visitor TraverseAST function is called. This is the function where the
    "tr", "trUtf8", "qtIdTr", "translate" functions are picked up in the AST.
    Previously mentioned functions are always part of a CallExpression.
*/
bool LupdateVisitor::VisitCallExpr(clang::CallExpr *callExpression)
{
    const auto fullLocation = m_context->getFullLoc(callExpression->getBeginLoc());
    if (fullLocation.isInvalid())
        return true;
    clang::FunctionDecl *func = callExpression->getDirectCallee();
    if (!func)
        return true;
    clang::QualType q = callExpression->getType();
    if (!q.getTypePtrOrNull())
        return true;

    struct {
        unsigned Line;
        std::string Filename;
    } info;

    const auto funcName = QString::fromStdString(func->getNameInfo().getAsString());

    // Only continue if the function a translation function (TODO: deal with alias function...)
    switch (trFunctionAliasManager.trFunctionByName(funcName)) {
    case TrFunctionAliasManager::Function_tr:
    case TrFunctionAliasManager::Function_trUtf8:
    case TrFunctionAliasManager::Function_translate:
    case TrFunctionAliasManager::Function_qtTrId:{

        const auto &sm = m_context->getSourceManager();
        const auto fileLoc = sm.getFileLoc(callExpression->getBeginLoc());
        if (fileLoc.isInvalid() || !fileLoc.isFileID())
            return true;
        auto presumedLoc = sm.getPresumedLoc(fileLoc);
        if (presumedLoc.isInvalid())
            return true;
        info = { presumedLoc.getLine(), presumedLoc.getFilename() };
    }   break;
    default:
        return true;
    }

    // Checking that the CallExpression is from the input file we're interested in
    if (info.Filename != m_inputFile)
        return true;

    qCDebug(lcClang) << "************************** VisitCallExpr ****************";
    // Retrieving the information needed to fill the lupdate translator.
    // Function independent retrieve
    TranslationRelatedStore store;
    store.callType = QStringLiteral("ASTRead_CallExpr");
    store.funcName = funcName;
    store.lupdateLocationFile = QString::fromStdString(info.Filename);
    store.lupdateLocationLine = info.Line;
    store.contextRetrieved = LupdatePrivate::contextForFunctionDecl(func, funcName.toStdString());

    qCDebug(lcClang) << "CallType          : ASTRead_CallExpr";
    qCDebug(lcClang) << "Function name     : " << store.funcName;
    qCDebug(lcClang) << "File location     : " << store.lupdateLocationFile;
    qCDebug(lcClang) << "Line              : " << store.lupdateLocationLine;
    qCDebug(lcClang) << "Context retrieved : " << store.contextRetrieved;

    // Here we gonna need to retrieve the comments around the function call
    // //: //* //~ Things like that
    const std::vector<QString> rawComments = rawCommentsForCallExpr(callExpression);
    for (const auto &rawComment : rawComments) {
        setInfoFromRawComment(rawComment, &store);
        qCDebug(lcClang) << "Raw comments     :" << rawComment;
    }

    clang::LangOptions langOpts;
    langOpts.CPlusPlus = true;
    clang::PrintingPolicy policy(langOpts);
    std::vector<std::string> arguments(callExpression->getNumArgs(), "");
    for (unsigned int i = 0; i < callExpression->getNumArgs(); i++) {
        auto arg = callExpression->getArg(i);
        llvm::raw_string_ostream temp(arguments[i]);
        arg->printPretty(temp, nullptr, policy);
    }

    // Function dependent retrieve!
    switch (trFunctionAliasManager.trFunctionByName(funcName)) {
    case TrFunctionAliasManager::Function_tr:
    case TrFunctionAliasManager::Function_trUtf8:
        if (arguments.size() != 3 || !LupdatePrivate::hasQuote(arguments[0]))
            return true;
        store.lupdateSource = LupdatePrivate::cleanQuote(arguments[0]);
        store.lupdateComment = LupdatePrivate::cleanQuote(arguments[1]);
        store.lupdatePlural = QString::fromStdString(arguments[2]);
        qCDebug(lcClang) << "Source      : " << store.lupdateSource;
        qCDebug(lcClang) << "Comment     : " << store.lupdateComment;
        qCDebug(lcClang) << "Plural      : " << store.lupdatePlural;
        break;
    case TrFunctionAliasManager::Function_translate:
        if (arguments.size() != 4 || !LupdatePrivate::hasQuote(arguments[0])
            || !LupdatePrivate::hasQuote(arguments[1])) {
            return true;
        }
        store.contextArg = LupdatePrivate::cleanQuote(arguments[0]);
        store.lupdateSource = LupdatePrivate::cleanQuote(arguments[1]);
        store.lupdateComment = LupdatePrivate::cleanQuote(arguments[2]);
        store.lupdatePlural = QString::fromStdString(arguments[3]);
        qCDebug(lcClang) << "Context Arg : " << store.contextArg;
        qCDebug(lcClang) << "Source      : " << store.lupdateSource;
        qCDebug(lcClang) << "Comment     : " << store.lupdateComment;
        qCDebug(lcClang) << "Plural      : " << store.lupdatePlural;
        break;
    case TrFunctionAliasManager::Function_qtTrId:
        if (arguments.size() != 2 || !LupdatePrivate::hasQuote(arguments[0]))
            return true;
        store.lupdateId = LupdatePrivate::cleanQuote(arguments[0]);
        store.lupdatePlural = QString::fromStdString(arguments[1]);
        qCDebug(lcClang) << "ID          : " << store.lupdateId;
        qCDebug(lcClang) << "Plural      : " << store.lupdatePlural;
        break;
    }
    // locationCol needs to be set for the store to be considered valid (but really only needed for PP calls, to reconstruct location)
    store.locationCol = 0;
    m_trCalls.emplace_back(std::move(store));
    return true;
}

/*
    Retrieve the comments not associated with tr calls.
*/
void LupdateVisitor::processIsolatedComments()
{
    qCDebug(lcClang) << "==== processIsolatedComments ====";
    auto &sourceMgr = m_context->getSourceManager();

#if (LUPDATE_CLANG_VERSION >= LUPDATE_CLANG_VERSION_CHECK(10,0,0))
    const clang::FileID file = sourceMgr.getMainFileID();
    const auto commentsInThisFile = m_context->getRawCommentList().getCommentsInFile(file);
    if (!commentsInThisFile)
        return;

    std::vector<clang::RawComment *> tmp;
    for (const auto &commentInFile : *commentsInThisFile)
        tmp.emplace_back(commentInFile.second);
    clang::ArrayRef<clang::RawComment *> rawComments = tmp;
#else
    clang::ArrayRef<clang::RawComment *> rawComments = m_context->getRawCommentList().getComments();
#endif

    // If there are no comments anywhere, we won't find anything.
    if (rawComments.empty())
        return;

    // Searching for the comments of the form:
    // /*  TRANSLATOR CONTEXT
    //     whatever */
    // They are not associated to any tr calls
    // Each one needs its own entry in the m_stores->AST translation store
    for (const auto &rawComment : rawComments) {
        if (sourceMgr.getFilename(rawComment->getBeginLoc()).str() != m_inputFile)
            continue;
        // Comments not separated by an empty line will be part of the same Raw comments
        // Each one needs to be saved with its line number.
        // The store is used here only to pass this information.
        TranslationRelatedStore store;
        store.lupdateLocationLine = sourceMgr.getPresumedLoc(rawComment->getBeginLoc()).getLine();
        qCDebug(lcClang) << " raw Comment : \n"
            << QString::fromStdString(rawComment->getRawText(sourceMgr));
        setInfoFromRawComment(QString::fromStdString(rawComment->getRawText(sourceMgr)), &store);
    }
}

/*
    Retrieve the comments associated with the CallExpression.
*/
std::vector<QString> LupdateVisitor::rawCommentsForCallExpr(const clang::CallExpr *callExpr) const
{
    if (!m_context)
        return {};
    return rawCommentsFromSourceLocation(m_context->getFullLoc(callExpr->getBeginLoc()));
}

std::vector<QString> LupdateVisitor::rawCommentsFromSourceLocation(
    clang::SourceLocation sourceLocation) const
{
    if (!m_context)
        return {};
    if (sourceLocation.isInvalid() || !sourceLocation.isFileID()) {
        qCDebug(lcClang) << "The declaration does not map directly to a location in a file,"
            " early return.";
        return {};
    }
    auto &sourceMgr = m_context->getSourceManager();

#if (LUPDATE_CLANG_VERSION >= LUPDATE_CLANG_VERSION_CHECK(10,0,0))
    const clang::FileID file = sourceMgr.getDecomposedLoc(sourceLocation).first;
    const auto commentsInThisFile = m_context->getRawCommentList().getCommentsInFile(file);
    if (!commentsInThisFile)
        return {};

    std::vector<clang::RawComment *> tmp;
    for (const auto &commentInFile : *commentsInThisFile)
        tmp.emplace_back(commentInFile.second);
    clang::ArrayRef<clang::RawComment *> rawComments = tmp;
#else
    clang::ArrayRef<clang::RawComment *> rawComments = m_context->getRawCommentList().getComments();
#endif

    // If there are no comments anywhere, we won't find anything.
    if (rawComments.empty())
        return {};

    // Create a dummy raw comment with the source location of the declaration.
    clang::RawComment commentAtDeclarationLocation(sourceMgr,
        clang::SourceRange(sourceLocation), m_context->getLangOpts().CommentOpts, false);

    // Create a functor object to compare the source location of the comment and the declaration.
    const clang::BeforeThanCompare<clang::RawComment> compareSourceLocation(sourceMgr);
    //  Find the comment that occurs just after or within this declaration. Possible findings:
    //  QObject::tr(/* comment 1 */ "test"); //: comment 2   -> finds "//: comment 1"
    //  QObject::tr("test"); //: comment 1                   -> finds "//: comment 1"
    //  QObject::tr("test");
    //  //: comment 1                                        -> finds "//: comment 1"
    //  /*: comment 1 */ QObject::tr("test");                -> finds no trailing comment
    auto comment = std::lower_bound(rawComments.begin(), rawComments.end(),
        &commentAtDeclarationLocation, compareSourceLocation);

    // We did not find any comment before the declaration.
    if (comment == rawComments.begin())
        return {};

    // Decompose the location for the declaration and find the beginning of the file buffer.
    std::pair<clang::FileID, unsigned> declLocDecomp = sourceMgr.getDecomposedLoc(sourceLocation);

    // Get the text buffer from the beginning of the file up through the declaration's begin.
    bool invalid = false;
    const char *buffer = sourceMgr.getBufferData(declLocDecomp.first, &invalid).data();
    if (invalid) {
        qCDebug(lcClang).nospace() << "An error occurred fetching the source buffer of file: "
            << sourceMgr.getFilename(sourceLocation);
        return {};
    }

    std::vector<QString> retrievedRawComments;
    auto lastDecompLoc = declLocDecomp.second;
    const auto declLineNum = sourceMgr.getLineNumber(declLocDecomp.first, declLocDecomp.second);
    do {
        std::advance(comment, -1);

        // Decompose the end of the comment.
        std::pair<clang::FileID, unsigned> commentEndDecomp
            = sourceMgr.getDecomposedLoc((*comment)->getSourceRange().getEnd());

        // If the comment and the declaration aren't in the same file, then they aren't related.
        if (declLocDecomp.first != commentEndDecomp.first) {
            qCDebug(lcClang) << "Comment and the declaration aren't in the same file. Comment '"
                << (*comment)->getRawText(sourceMgr) << "' is ignored, return.";
            return retrievedRawComments;
        }

        // Current lupdate ignores comments on the same line before the declaration.
        // void Class42::hello(int something /*= 17 */, QString str = Class42::tr("eyo"))
        bool sameLineComment = false;
        if (declLineNum == sourceMgr.getLineNumber(commentEndDecomp.first, commentEndDecomp.second))
            sameLineComment = true;

        // Extract text between the comment and declaration.
        llvm::StringRef text(buffer + commentEndDecomp.second,
            lastDecompLoc - commentEndDecomp.second);

        // There should be no other declarations or preprocessor directives between
        // comment and declaration.
        if (text.find_first_of(";}#@") != llvm::StringRef::npos) {
            qCDebug(lcClang) << "Found another declaration or preprocessor directive between"
                " comment and declaration, break.";
            break;
        }
        if (sameLineComment && text.find_first_of(",") != llvm::StringRef::npos) {
            qCDebug(lcClang) << "Comment ends on same line as the declaration and is separated "
            "from the tr call by a ','. Comment '"
                << (*comment)->getRawText(sourceMgr) << "' is ignored, continue.";
            continue; // if there is a comment on the previous line it should be picked up
        }

        // There should be no other translation function between comment and declaration.
        if (LupdatePrivate::trFunctionPresent(text)) {
            qCDebug(lcClang) << "Found another translation function between comment and "
                "declaration, break.";
            break;
        }

        retrievedRawComments.emplace(retrievedRawComments.begin(),
            QString::fromStdString((*comment)->getRawText(sourceMgr)));
        lastDecompLoc = sourceMgr.getDecomposedLoc((*comment)->getSourceRange().getBegin()).second;
    } while (comment != rawComments.begin());

    return retrievedRawComments;
}

/*
    Read the raw comments and split them according to the prefix.
    Fill the corresponding variables in the TranslationRelatedStore.
*/
void LupdateVisitor::setInfoFromRawComment(const QString &commentString,
    TranslationRelatedStore *store)
{
    const QStringList commentLines = commentString.split(QLatin1Char('\n'));

    static const QRegularExpression
        cppStyle(
        QStringLiteral("^\\/\\/(?<identifier>[:=~%]|(\\s*?TRANSLATOR))\\s+(?<comment>.+)$"));
    static const QRegularExpression
        cStyleSingle(
        QStringLiteral("^\\/\\*(?<identifier>[:=~%]|(\\s*?TRANSLATOR))\\s+(?<comment>.+)\\*\\/$"));
    static const QRegularExpression
        cStyleMultiBegin(
        QStringLiteral("^\\/\\*(?<identifier>[:=~%]|(\\s*?TRANSLATOR))\\s+(?<comment>.*)$"));

    static const QRegularExpression isSpace(QStringLiteral("\\s+"));
    static const QRegularExpression idefix(
        QStringLiteral("^\\/\\*(?<identifier>[:=~%]|(\\s*?TRANSLATOR))"));

    bool save = false;
    bool sawStarPrefix = false;
    bool sourceIdentifier = false;

    int storeLine = store->lupdateLocationLine;
    int lineExtra =  storeLine - 1;

    QString comment, identifier;
    for (auto line : commentLines) {
        line = line.trimmed();
        lineExtra++;
        if (!sawStarPrefix) {
            if (line.startsWith(QStringLiteral("//"))) {
                // Process C++ style comment.
                save = LupdatePrivate::capture(cppStyle, line, &identifier, &comment);
                storeLine = lineExtra;
            } else if (line.startsWith(QLatin1String("/*")) && line.endsWith(QLatin1String("*/"))) {
                // Process C style comment on a single line.
                storeLine = lineExtra;
                save = LupdatePrivate::capture(cStyleSingle, line, &identifier, &comment);
            } else if (line.startsWith(QLatin1String("/*"))) {
                storeLine = lineExtra;
                sawStarPrefix = true; // Start processing a multi line C style comment.

                auto result = idefix.match(line);
                if (!result.hasMatch())
                    continue; // No identifier found.
                identifier = result.captured(QLatin1String("identifier"));

                // The line is not just opening, try grab the comment.
                 if (line.size() > (identifier.size() + 3))
                    LupdatePrivate::capture(cStyleMultiBegin, line, &identifier, &comment);
                sourceIdentifier = (identifier == QLatin1String("%"));
            }
        } else {
            if (line.endsWith(QLatin1String("*/"))) {
                sawStarPrefix = false; // Finished processing a multi line C style comment.
                line = line.remove(QLatin1String("*/")).trimmed(); // Still there can be something.
            }

            if (sourceIdentifier) {
                line = LupdatePrivate::cleanQuote(line.toStdString(),
                    LupdatePrivate::QuoteCompulsary::Left);
            }

            if (!line.isEmpty() && !comment.isEmpty() && !sourceIdentifier)
                comment.append(QLatin1Char(' '));

            comment += line;
            save = !sawStarPrefix && !comment.isEmpty();
        }
        if (!save)
            continue;

        // To avoid processing the non TRANSLATOR comments when setInfoFromRawComment in called
        // from processIsolatedComments
        if (!store->funcName.isEmpty()) {
            if (identifier == QStringLiteral(":")) {
                if (!store->lupdateExtraComment.isEmpty())
                    store->lupdateExtraComment.append(QLatin1Char(' '));
                store->lupdateExtraComment += comment;
            } else if (identifier == QStringLiteral("=")) {
                if (!store->lupdateIdMetaData.isEmpty())
                    store->lupdateIdMetaData.append(QLatin1Char(' '));
                store->lupdateIdMetaData = comment; // Only the last one is to be picked up.
            } else if (identifier == QStringLiteral("~")) {
                auto first = comment.section(isSpace, 0, 0);
                auto second = comment.mid(first.size()).trimmed();
                if (!second.isEmpty())
                    store->lupdateAllMagicMetaData.insert(first, second);
            } else if (identifier == QLatin1String("%")) {
                store->lupdateSourceWhenId += comment;
            }
        } else if (identifier.trimmed() == QStringLiteral("TRANSLATOR")) {
            // separate the comment in two in order to get the context
            // then save it as a new entry in m_stores.
            // reminder: TRANSLATOR comments are isolated comments not linked to any tr call
            qCDebug(lcClang) << "Comment = " << comment;
            TranslationRelatedStore newStore;
            // need a funcName name in order to get handeled in fillTranslator
            newStore.funcName = QStringLiteral("TRANSLATOR");
            auto index = comment.indexOf(QStringLiteral(" "));
            if (index >= 0) {
                newStore.contextArg = comment.left(index).trimmed();
                newStore.lupdateComment = comment.mid(index).trimmed();
            }
            newStore.lupdateLocationFile = QString::fromStdString(m_inputFile);
            newStore.lupdateLocationLine = storeLine;
            newStore.locationCol = 0;
            newStore.printStore();
            m_trCalls.emplace_back(std::move(newStore));
        }

        save = false;
        comment.clear();
        identifier.clear();
    }
}

void LupdateVisitor::processPreprocessorCalls()
{
    m_macro = (m_stores->Preprocessor.size() > 0);
    for (const auto &store : m_stores->Preprocessor)
        processPreprocessorCall(store);
}

void LupdateVisitor::processPreprocessorCall(TranslationRelatedStore store)
{
    const std::vector<QString> rawComments = rawCommentsFromSourceLocation(store
        .callLocation(m_context->getSourceManager()));
    for (const auto &rawComment : rawComments)
        setInfoFromRawComment(rawComment, &store);

    if (store.isValid()) {
        if (store.funcName.contains(QStringLiteral("Q_DECLARE_TR_FUNCTIONS")))
            m_qDeclareTrMacroAll.emplace_back(std::move(store));
        else
            m_noopTranslationMacroAll.emplace_back(std::move(store));
        store.printStore();
    }
}

bool LupdateVisitor::VisitNamedDecl(clang::NamedDecl *namedDeclaration)
{
    if (!m_macro)
        return true;
    auto fullLocation = m_context->getFullLoc(namedDeclaration->getBeginLoc());
    if (!fullLocation.isValid() || !fullLocation.getFileEntry())
        return true;

    if (fullLocation.getFileEntry()->getName() != m_inputFile)
        return true;

    qCDebug(lcClang) << "NamedDecl Name:   " << namedDeclaration->getQualifiedNameAsString();
    qCDebug(lcClang) << "NamedDecl source: " << namedDeclaration->getSourceRange().printToString(
        m_context->getSourceManager());
    // Checks if there is a macro located within the range of this NamedDeclaration
    // in order to find a context for the macro
    findContextForTranslationStoresFromPP(namedDeclaration);
    return true;
}

void LupdateVisitor::findContextForTranslationStoresFromPP(clang::NamedDecl *namedDeclaration)
{
    qCDebug(lcClang) << "=================findContextForTranslationStoresFromPP===================";
    qCDebug(lcClang) << "m_noopTranslationMacroAll " << m_noopTranslationMacroAll.size();
    qCDebug(lcClang) << "m_qDeclareTrMacroAll      " << m_qDeclareTrMacroAll.size();
    clang::SourceManager &sm = m_context->getSourceManager();

    // Looking for NOOP context only in the input file
    // because we are not interested in the NOOP from all related file
    // Once QT_TR_NOOP are gone this step can be removes because the only
    // QT_...NOOP left will have an context as argument
    for (TranslationRelatedStore &store : m_noopTranslationMacroAll) {
        if (!store.contextArg.isEmpty())
            continue;
        clang::SourceLocation sourceLoc = store.callLocation(sm);
        if (!sourceLoc.isValid())
            continue;
        if (LupdatePrivate::isPointWithin(namedDeclaration->getSourceRange(), sourceLoc, sm)) {
            /*
                void N3::C1::C12::C121::f2()
                {
                    const char test_NOOP[] = QT_TR_NOOP("A QT_TR_NOOP N3::C1::C13");
                }
                In such case namedDeclaration->getQualifiedNameAsString() will give only
                test_NOOP as context.
                This is why the following function is needed
            */
            store.contextRetrievedTempNOOP = LupdatePrivate::contextForNoopMacro(namedDeclaration);
            qCDebug(lcClang) << "------------------------------------------NOOP Macro in range ---";
            qCDebug(lcClang) << "Range " << namedDeclaration->getSourceRange().printToString(sm);
            qCDebug(lcClang) << "Point " << sourceLoc.printToString(sm);
            qCDebug(lcClang) << "=========== Visit Named Declaration =============================";
            qCDebug(lcClang) << " Declaration Location    " <<
                namedDeclaration->getSourceRange().printToString(sm);
            qCDebug(lcClang) << " Macro       Location                                 "
                << sourceLoc.printToString(sm);
            qCDebug(lcClang) << " Context namedDeclaration->getQualifiedNameAsString() "
                << namedDeclaration->getQualifiedNameAsString();
            qCDebug(lcClang) << " Context LupdatePrivate::contextForNoopMacro          "
                << store.contextRetrievedTempNOOP;
            qCDebug(lcClang) << " Context Retrieved       " << store.contextRetrievedTempNOOP;
            qCDebug(lcClang) << "=================================================================";
            store.printStore();
        }
    }

    for (TranslationRelatedStore &store : m_qDeclareTrMacroAll) {
        clang::SourceLocation sourceLoc = store.callLocation(sm);
        if (!sourceLoc.isValid())
            continue;
        if (LupdatePrivate::isPointWithin(namedDeclaration->getSourceRange(), sourceLoc, sm)) {
            store.contextRetrieved = QString::fromStdString(
                namedDeclaration->getQualifiedNameAsString());
            qCDebug(lcClang) << "------------------------------------------DECL Macro in range ---";
            qCDebug(lcClang) << "Range " << namedDeclaration->getSourceRange().printToString(sm);
            qCDebug(lcClang) << "Point " << sourceLoc.printToString(sm);
            qCDebug(lcClang) << "=========== Visit Named Declaration =============================";
            qCDebug(lcClang) << " Declaration Location    " <<
                namedDeclaration->getSourceRange().printToString(sm);
            qCDebug(lcClang) << " Macro       Location                                 "
                << sourceLoc.printToString(sm);
            qCDebug(lcClang) << " Context namedDeclaration->getQualifiedNameAsString() "
                << store.contextRetrieved;
            qCDebug(lcClang) << " Context LupdatePrivate::contextForNoopMacro          "
                << LupdatePrivate::contextForNoopMacro(namedDeclaration);
            qCDebug(lcClang) << " Context Retrieved       " << store.contextRetrieved;
            qCDebug(lcClang) << "=================================================================";
            store.printStore();
        }
    }
}

void LupdateVisitor::generateOuput()
{
    qCDebug(lcClang) << "=================m_trCallserateOuput============================";
    m_noopTranslationMacroAll.erase(std::remove_if(m_noopTranslationMacroAll.begin(),
          m_noopTranslationMacroAll.end(), [](const TranslationRelatedStore &store) {
              // only fill if a context has been retrieved in the file we're currently visiting
              return store.contextRetrievedTempNOOP.isEmpty() && store.contextArg.isEmpty();
      }), m_noopTranslationMacroAll.end());

    m_stores->QNoopTranlsationWithContext.emplace_bulk(std::move(m_noopTranslationMacroAll));

    m_qDeclareTrMacroAll.erase(std::remove_if(m_qDeclareTrMacroAll.begin(),
          m_qDeclareTrMacroAll.end(), [](const TranslationRelatedStore &store) {
              // only fill if a context has been retrieved in the file we're currently visiting
              return store.contextRetrieved.isEmpty();
      }), m_qDeclareTrMacroAll.end());
    m_stores->QDeclareTrWithContext.emplace_bulk(std::move(m_qDeclareTrMacroAll));

    processIsolatedComments();
    m_stores->AST.emplace_bulk(std::move(m_trCalls));
}

QT_END_NAMESPACE