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
|
// This file is part of the AspectC++ compiler 'ac++'.
// Copyright (C) 1999-2003 The 'ac++' developers (see aspectc.org)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
#include "ClangPreprocessor.h"
#include "IncludeGraph.h"
#include "IntroductionUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
using namespace clang;
ClangPreprocessor::ClangPreprocessor (const string &tunit_name,
ACConfig &conf, IncludeGraph &include_graph, ACProject &project) :
_conf(conf), _project(project), _tunit_name(tunit_name) {
CompilerInstance &CI = *_project.get_compiler_instance();
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && !defined(CLANG_VERSION_PATCHLEVEL)) || \
(CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && CLANG_VERSION_PATCHLEVEL == 2)
CI.createPreprocessor();
#else // C++ 11 interface
CI.createPreprocessor(clang::TU_Complete);
#endif
Preprocessor &PP = CI.getPreprocessor();
CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), &PP);
#if FRONTEND_CLANG < 38
PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
PP.getLangOpts());
#else
// new interface (lowercase 'I')
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
PP.getLangOpts());
#endif
CI.InitializeSourceManager(FrontendInputFile(tunit_name, IK_CXX));
PP.EnterMainSourceFile();
#if (CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && !defined(CLANG_VERSION_PATCHLEVEL)) || \
(CLANG_VERSION_MAJOR == 3 && CLANG_VERSION_MINOR == 4 && CLANG_VERSION_PATCHLEVEL == 2)
PP.addPPCallbacks(new IncludeGraph::IncludeGraphCallback(include_graph));
#else // C++ 11 interface
PP.addPPCallbacks(std::unique_ptr<IncludeGraph::IncludeGraphCallback>(new IncludeGraph::IncludeGraphCallback(include_graph)));
#endif
next_token();
}
ClangPreprocessor::~ClangPreprocessor () {
CompilerInstance &CI = *_project.get_compiler_instance();
CI.getDiagnosticClient().EndSourceFile();
CI.getPreprocessor().EndSourceFile();
}
bool ClangPreprocessor::keywords_enabled (clang::SourceLocation loc) const {
SourceManager &SM = _project.get_compiler_instance()->getSourceManager();
ACFileID funit = SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc (loc)));
if (!funit.is_valid() || !_project.isBelow(funit.name().c_str ()))
return false;
string name = funit.name ();
size_t len = name.length();
if (len > 2 && strcmp (name.c_str () + (len - 3), ".ah") == 0)
return true;
return _conf.keywords();
}
ACErrorStream &ClangPreprocessor::err () const {
return _project.err ();
}
ACToken ClangPreprocessor::next_token () {
Preprocessor &PP = _project.get_compiler_instance()->getPreprocessor();
clang::Token tok;
PP.Lex(tok);
_current_token = ACToken(tok, keywords_enabled(tok.getLocation()));
if (!_recorded_tokens.empty())
_recorded_tokens.push_back(_current_token);
return _current_token;
}
ACToken ClangPreprocessor::curr_token () {
return _current_token;
}
ACToken ClangPreprocessor::look_ahead (int n) {
Preprocessor &PP = _project.get_compiler_instance()->getPreprocessor();
clang::Token Tok(PP.LookAhead(n - 1));
return ACToken(Tok, keywords_enabled(Tok.getLocation()));
}
// functions to record the token stream
void ClangPreprocessor::start_recording () {
if (_recorded_tokens.empty())
_recorded_tokens.push_back(curr_token());
}
ClangPreprocessor::TokenVector ClangPreprocessor::stop_recording () {
TokenVector result;
std::swap(result, _recorded_tokens);
return result;
}
ACFileID ClangPreprocessor::source_unit (ACToken token) const {
SourceManager &SM = _project.get_compiler_instance()->getSourceManager();
return SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc (token.location ())));
}
// check whether a token is located within the files of the project
bool ClangPreprocessor::is_in_project(ACToken token) const {
ACFileID funit = source_unit(token);
return funit.is_valid() && _project.isBelow(funit.name().c_str ());
}
std::string ClangPreprocessor::token_text (ACToken tok) const {
Preprocessor &PP = _project.get_compiler_instance()->getPreprocessor();
return PP.getSpelling(tok.get());
}
unsigned ClangPreprocessor::token_line_number (ACToken tok) const {
SourceManager &SM = _project.get_compiler_instance()->getSourceManager();
return SM.getPresumedLineNumber(tok.location());
}
|