2016-08-04 15:26:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "sourcelocationsutils.h"
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
#include <filepathcachingfwd.h>
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <sourcelocationscontainer.h>
|
|
|
|
|
|
|
|
|
|
#include <clang/Basic/SourceManager.h>
|
|
|
|
|
#include <clang/Lex/PPCallbacks.h>
|
|
|
|
|
#include <clang/Lex/Preprocessor.h>
|
|
|
|
|
#include <clang/Lex/MacroInfo.h>
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
|
|
|
|
struct MacroDirectiveToken
|
|
|
|
|
{
|
|
|
|
|
MacroDirectiveToken(const clang::MacroDirective *macroDirective,
|
|
|
|
|
const clang::Token &token)
|
|
|
|
|
: macroDirective(macroDirective),
|
|
|
|
|
token(token)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
const clang::MacroDirective *macroDirective;
|
|
|
|
|
const clang::Token token;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MacroPreprocessorCallbacks : public clang::PPCallbacks
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-09-21 11:43:59 +02:00
|
|
|
MacroPreprocessorCallbacks(SourceLocationsContainer &m_sourceLocationsContainer,
|
|
|
|
|
Utils::SmallString &m_symbolName,
|
|
|
|
|
clang::Preprocessor &m_preprocessor,
|
|
|
|
|
FilePathCachingInterface &filePathCache,
|
|
|
|
|
uint m_line,
|
|
|
|
|
uint m_column);
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
void FileChanged(clang::SourceLocation location,
|
|
|
|
|
FileChangeReason reason,
|
|
|
|
|
clang::SrcMgr::CharacteristicKind /*fileType*/,
|
|
|
|
|
clang::FileID /*previousFileIdentifier*/) final
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
if (!m_isMainFileEntered) {
|
2016-08-04 15:26:53 +02:00
|
|
|
updateLocations();
|
|
|
|
|
updateIsMainFileEntered(location, reason);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroDefined(const clang::Token &token, const clang::MacroDirective *macroDirective) final
|
|
|
|
|
{
|
|
|
|
|
if (isInMainFile(token)) {
|
|
|
|
|
if (includesCursorPosition(token)) {
|
2017-09-21 11:43:59 +02:00
|
|
|
m_sourceLocations.push_back(token.getLocation());
|
|
|
|
|
m_cursorMacroDirective = macroDirective;
|
|
|
|
|
m_symbolName = Utils::SmallString(token.getIdentifierInfo()->getNameStart(),
|
2016-08-04 15:26:53 +02:00
|
|
|
token.getIdentifierInfo()->getLength());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroExpands(const clang::Token &token,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition,
|
|
|
|
|
clang::SourceRange /*sourceRange*/,
|
|
|
|
|
const clang::MacroArgs * /*macroArguments*/) final
|
|
|
|
|
{
|
|
|
|
|
if (includesCursorPosition(token)) {
|
|
|
|
|
appendSourceLocations(token, macroDefinition);
|
2017-09-21 11:43:59 +02:00
|
|
|
m_cursorMacroDirective = macroDefinition.getLocalDirective();
|
|
|
|
|
m_symbolName = Utils::SmallString(token.getIdentifierInfo()->getNameStart(),
|
2016-08-04 15:26:53 +02:00
|
|
|
token.getIdentifierInfo()->getLength());
|
|
|
|
|
} else if (isCurrentTokenExpansion(macroDefinition)) {
|
2017-09-21 11:43:59 +02:00
|
|
|
m_sourceLocations.push_back(token.getLocation());
|
2016-08-04 15:26:53 +02:00
|
|
|
} else if (isBeforeCursorSourceLocation()) {
|
2017-09-21 11:43:59 +02:00
|
|
|
m_preCursorMacroDirectiveTokens.emplace_back(macroDefinition.getLocalDirective(), token);
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EndOfMainFile() final
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
appendSourceLocationsToSourceLocationsContainer(m_sourceLocationsContainer,
|
|
|
|
|
m_sourceLocations,
|
|
|
|
|
sourceManager(),
|
|
|
|
|
m_filePathCache);
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void appendSourceLocations(const clang::Token &token,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition)
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
m_sourceLocations.push_back(macroDefinition.getLocalDirective()->getLocation());
|
|
|
|
|
for (const auto ¯oDirectiveToken : m_preCursorMacroDirectiveTokens) {
|
2016-08-04 15:26:53 +02:00
|
|
|
if (macroDirectiveToken.macroDirective == macroDefinition.getLocalDirective())
|
2017-09-21 11:43:59 +02:00
|
|
|
m_sourceLocations.push_back(macroDirectiveToken.token.getLocation());
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
2017-09-21 11:43:59 +02:00
|
|
|
m_sourceLocations.push_back(token.getLocation());
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateLocations()
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
if (m_mainFileSourceLocation.isInvalid()) {
|
|
|
|
|
m_mainFileSourceLocation = sourceManager().getLocForStartOfFile(sourceManager().getMainFileID());
|
|
|
|
|
m_cursorSourceLocation = sourceManager().translateLineCol(sourceManager().getMainFileID(),
|
|
|
|
|
m_line,
|
|
|
|
|
m_column);
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateIsMainFileEntered(clang::SourceLocation location, FileChangeReason reason)
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
if (location == m_mainFileSourceLocation && reason == PPCallbacks::EnterFile)
|
|
|
|
|
m_isMainFileEntered = true;
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const clang::SourceManager &sourceManager() const
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
return m_preprocessor.getSourceManager();
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isInMainFile(const clang::Token &token)
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
return m_isMainFileEntered && sourceManager().isWrittenInMainFile(token.getLocation());
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool includesCursorPosition(const clang::Token &token)
|
|
|
|
|
{
|
|
|
|
|
auto start = token.getLocation();
|
|
|
|
|
auto end = token.getEndLoc();
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
return m_cursorSourceLocation == start
|
|
|
|
|
|| m_cursorSourceLocation == end
|
|
|
|
|
|| (sourceManager().isBeforeInTranslationUnit(start, m_cursorSourceLocation) &&
|
|
|
|
|
sourceManager().isBeforeInTranslationUnit(m_cursorSourceLocation, end));
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isCurrentTokenExpansion(const clang::MacroDefinition ¯oDefinition)
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
return m_cursorMacroDirective
|
|
|
|
|
&& m_cursorMacroDirective == macroDefinition.getLocalDirective();
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isBeforeCursorSourceLocation() const
|
|
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
return !m_cursorMacroDirective;
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2017-09-21 11:43:59 +02:00
|
|
|
std::vector<clang::SourceLocation> m_sourceLocations;
|
|
|
|
|
std::vector<MacroDirectiveToken> m_preCursorMacroDirectiveTokens;
|
|
|
|
|
SourceLocationsContainer &m_sourceLocationsContainer;
|
|
|
|
|
Utils::SmallString &m_symbolName;
|
|
|
|
|
clang::Preprocessor &m_preprocessor;
|
|
|
|
|
const clang::MacroDirective *m_cursorMacroDirective = nullptr;
|
|
|
|
|
clang::SourceLocation m_mainFileSourceLocation;
|
|
|
|
|
clang::SourceLocation m_cursorSourceLocation;
|
|
|
|
|
FilePathCachingInterface &m_filePathCache;
|
|
|
|
|
uint m_line;
|
|
|
|
|
uint m_column;
|
|
|
|
|
bool m_isMainFileEntered = false;
|
2016-08-04 15:26:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|