diff --git a/include/cppast/libclang_parser.hpp b/include/cppast/libclang_parser.hpp index 6efcee7..3c65d09 100644 --- a/include/cppast/libclang_parser.hpp +++ b/include/cppast/libclang_parser.hpp @@ -27,6 +27,8 @@ namespace cppast static bool write_preprocessed(const libclang_compile_config& config); static bool fast_preprocessing(const libclang_compile_config& config); + + static bool remove_comments_in_macro(const libclang_compile_config& config); }; void for_each_file(const libclang_compilation_database& database, void* user_data, @@ -144,6 +146,15 @@ namespace cppast fast_preprocessing_ = b; } + /// \effects Sets whether or not documentation comments generated by macros are removed. + /// Default value is `false`. + /// \notes If this leads to an error due to preprocessing and comments, you have to enable it. + /// \notes If this is `true`, `clang` will be invoked with `-CC`, otherwise `-C`. + void remove_comments_in_macro(bool b) noexcept + { + remove_comments_in_macro_ = b; + } + private: void do_set_flags(cpp_standard standard, compile_flags flags) override; @@ -162,6 +173,7 @@ namespace cppast int clang_version_; bool write_preprocessed_ : 1; bool fast_preprocessing_ : 1; + bool remove_comments_in_macro_ : 1; friend detail::libclang_compile_config_access; }; diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index 10730cf..3edecd7 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -48,6 +48,12 @@ bool detail::libclang_compile_config_access::fast_preprocessing( return config.fast_preprocessing_; } +bool detail::libclang_compile_config_access::remove_comments_in_macro( + const libclang_compile_config& config) +{ + return config.remove_comments_in_macro_; +} + libclang_compilation_database::libclang_compilation_database(const std::string& build_directory) { static_assert(std::is_same::value, "forgot to update type"); @@ -88,7 +94,10 @@ namespace } libclang_compile_config::libclang_compile_config() -: compile_config({}), write_preprocessed_(false), fast_preprocessing_(false) +: compile_config({}), + write_preprocessed_(false), + fast_preprocessing_(false), + remove_comments_in_macro_(false) { // set given clang binary auto ptr = CPPAST_CLANG_VERSION_STRING; diff --git a/src/libclang/preprocessor.cpp b/src/libclang/preprocessor.cpp index b43fa6f..9ccdcad 100644 --- a/src/libclang/preprocessor.cpp +++ b/src/libclang/preprocessor.cpp @@ -200,9 +200,15 @@ namespace { // -x c++: force C++ as input language // -E: print preprocessor output - // -CC: keep comments, even in macro // -dD: keep macros - auto flags = std::string("-x c++ -E -CC -dD"); + auto flags = std::string("-x c++ -E -dD"); + + // -CC: keep comments, even in macro + // -C: keep comments, but not in macro + if (!detail::libclang_compile_config_access::remove_comments_in_macro(c)) + flags += " -CC"; + else + flags += " -C"; if (macro_file_path) // -no*: disable default include search paths diff --git a/tool/main.cpp b/tool/main.cpp index 8e9ae36..8360e8e 100644 --- a/tool/main.cpp +++ b/tool/main.cpp @@ -210,7 +210,8 @@ int main(int argc, char* argv[]) try ("gnu_extensions", "enable GNU extensions (equivalent to -std=gnu++XX)") ("msvc_extensions", "enable MSVC extensions (equivalent to -fms-extensions)") ("msvc_compatibility", "enable MSVC compatibility (equivalent to -fms-compatibility)") - ("fast_preprocessing", "enable fast preprocessing, be careful, this breaks if you e.g. redefine macros in the same file!"); + ("fast_preprocessing", "enable fast preprocessing, be careful, this breaks if you e.g. redefine macros in the same file!") + ("remove_comments_in_macro", "whether or not comments generated by macro are kept, enable if you run into errors"); // clang-format on options.parse_positional("file"); options.parse(argc, argv); @@ -252,6 +253,9 @@ int main(int argc, char* argv[]) try if (options.count("fast_preprocessing")) config.fast_preprocessing(true); + if (options.count("remove_comments_in_macro")) + config.remove_comments_in_macro(true); + if (options.count("include_directory")) for (auto& include : options["include_directory"].as>()) config.add_include_dir(include);