|
| 1 | +/* |
| 2 | + * This file is part of arduino-preprocessor. |
| 3 | + * |
| 4 | + * Copyright 2017 BCMI LABS SA |
| 5 | + * |
| 6 | + * arduino-preprocessor is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + * |
| 20 | + * As a special exception, you may use this file as part of a free software |
| 21 | + * library without restriction. Specifically, if other files instantiate |
| 22 | + * templates or use macros or inline functions from this file, or you compile |
| 23 | + * this file and link it with other files to produce an executable, this |
| 24 | + * file does not by itself cause the resulting executable to be covered by |
| 25 | + * the GNU General Public License. This exception does not however |
| 26 | + * invalidate any other reasons why the executable file might be covered by |
| 27 | + * the GNU General Public License. |
| 28 | + */ |
| 29 | + |
| 30 | +#pragma once |
| 31 | + |
| 32 | +#include <clang/AST/ASTConsumer.h> |
| 33 | +#include <sstream> |
| 34 | + |
| 35 | +using namespace clang; |
| 36 | +using namespace std; |
| 37 | + |
| 38 | +#define JSON_NOEXCEPTION |
| 39 | +#include "json.hpp" |
| 40 | +#include "clang/include/clang/Basic/SourceManager.h" |
| 41 | +using json = nlohmann::json; |
| 42 | + |
| 43 | +template<unsigned InternalLen> |
| 44 | +inline string encode(const SmallString<InternalLen> &s) { |
| 45 | + return s.str().str(); |
| 46 | +} |
| 47 | + |
| 48 | +inline json encode(const SourceManager &sm, const SourceLocation &loc) { |
| 49 | + PresumedLoc presumed = sm.getPresumedLoc(loc); |
| 50 | + stringstream pos; |
| 51 | + pos << presumed.getLine() << ":" << presumed.getColumn(); |
| 52 | + return json{ |
| 53 | + {"file", presumed.getFilename()}, |
| 54 | + {"pos", pos.str()}}; |
| 55 | +} |
| 56 | + |
| 57 | +inline json encode(const SourceManager &sm, const CharSourceRange &range) { |
| 58 | + if (range.isInvalid()) { |
| 59 | + return json{}; |
| 60 | + } |
| 61 | + return json{ |
| 62 | + {"begin", encode(sm, range.getBegin())}, |
| 63 | + {"end", encode(sm, range.getEnd())}}; |
| 64 | +} |
| 65 | + |
| 66 | +inline json encode(const SourceManager &sm, const FixItHint &hint) { |
| 67 | + return json{ |
| 68 | + {"before_previous", hint.BeforePreviousInsertions}, |
| 69 | + {"text", hint.CodeToInsert}, |
| 70 | + {"insert_from", encode(sm, hint.InsertFromRange)}, |
| 71 | + {"remove", encode(sm, hint.RemoveRange)}}; |
| 72 | +} |
| 73 | + |
| 74 | +template<typename T> |
| 75 | +inline json encode(const SourceManager &sm, const ArrayRef<T> &array) { |
| 76 | + json res = json::array(); |
| 77 | + for (T elem : array) { |
| 78 | + res.push_back(encode(sm, elem)); |
| 79 | + } |
| 80 | + return res; |
| 81 | +} |
0 commit comments