Browse Source

Formatted code with custom clang-format

merge-requests/37/head
eidheim 8 years ago
parent
commit
791b76b04c
  1. 9
      .clang-format
  2. 30
      README.md
  3. 16
      src/clangmm.h
  4. 3
      src/code_complete_results.h
  5. 3
      src/compilation_database.h
  6. 4
      src/compile_command.h
  7. 2
      src/compile_commands.h
  8. 2
      src/completion_string.h
  9. 7
      src/cursor.cc
  10. 4
      src/cursor.h
  11. 11
      src/diagnostic.h
  12. 1
      src/source_location.cc
  13. 3
      src/source_location.h
  14. 2
      src/source_range.h
  15. 11
      src/token.h
  16. 2
      src/tokens.cc
  17. 6
      src/tokens.h
  18. 15
      src/translation_unit.h
  19. 3
      src/utility.cc
  20. 2
      src/utility.h
  21. 2
      tests/code_complete_results_test.cc
  22. 2
      tests/completion_string_test.cc
  23. 2
      tests/cursor_test.cc
  24. 4
      tests/diagnostics_test.cc
  25. 2
      tests/source_location_test.cc
  26. 2
      tests/token_test.cc
  27. 4
      tests/translation_unit_test.cc

9
.clang-format

@ -0,0 +1,9 @@
IndentWidth: 2
AccessModifierOffset: -2
UseTab: Never
ColumnLimit: 0
MaxEmptyLinesToKeep: 2
SpaceBeforeParens: Never
BreakBeforeBraces: Custom
BraceWrapping: {BeforeElse: true, BeforeCatch: true}
NamespaceIndentation: All

30
README.md

@ -13,7 +13,7 @@ Developed for [juCi++](https://gitlab.com/cppit/jucipp), a lightweight, platform
## Installation ## ## Installation ##
See [installation guide](https://gitlab.com/cppit/libclangmm/blob/master/docs/install.md) See [installation guide](https://gitlab.com/cppit/libclangmm/blob/master/docs/install.md)
# Tests # ## Tests ##
To run the unit tests: To run the unit tests:
```sh ```sh
mkdir build && cd build mkdir build && cd build
@ -21,3 +21,31 @@ cmake -DBUILD_TESTING=1 ..
make make
make test make test
``` ```
## Coding style
Due to poor lambda support in clang-format, a custom clang-format is used with the following patch applied:
```diff
diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp
index bb8efd61a3..e80a487055 100644
--- a/lib/Format/ContinuationIndenter.cpp
+++ b/lib/Format/ContinuationIndenter.cpp
@@ -276,6 +276,8 @@ LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
}
bool ContinuationIndenter::canBreak(const LineState &State) {
+ if(Style.ColumnLimit==0)
+ return true;
const FormatToken &Current = *State.NextToken;
const FormatToken &Previous = *Current.Previous;
assert(&Previous == Current.Previous);
@@ -325,6 +327,8 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
}
bool ContinuationIndenter::mustBreak(const LineState &State) {
+ if(Style.ColumnLimit==0)
+ return false;
const FormatToken &Current = *State.NextToken;
const FormatToken &Previous = *Current.Previous;
if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
```

16
src/clangmm.h

@ -1,17 +1,17 @@
#ifndef CLANGMM_H_ #ifndef CLANGMM_H_
#define CLANGMM_H_ #define CLANGMM_H_
#include "translation_unit.h" #include "code_complete_results.h"
#include "source_location.h"
#include "source_range.h"
#include "token.h"
#include "tokens.h"
#include "compilation_database.h" #include "compilation_database.h"
#include "compile_commands.h"
#include "compile_command.h" #include "compile_command.h"
#include "code_complete_results.h" #include "compile_commands.h"
#include "completion_string.h" #include "completion_string.h"
#include "index.h"
#include "cursor.h" #include "cursor.h"
#include "diagnostic.h" #include "diagnostic.h"
#include "index.h"
#include "source_location.h"
#include "source_range.h"
#include "token.h"
#include "tokens.h"
#include "translation_unit.h"
#include "utility.h" #include "utility.h"
#endif // CLANGMM_H_ #endif // CLANGMM_H_

3
src/code_complete_results.h

@ -1,9 +1,9 @@
#ifndef CODECOMPLETERESULTS_H_ #ifndef CODECOMPLETERESULTS_H_
#define CODECOMPLETERESULTS_H_ #define CODECOMPLETERESULTS_H_
#include "completion_string.h"
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include <map> #include <map>
#include <string> #include <string>
#include "completion_string.h"
namespace clangmm { namespace clangmm {
class CodeCompleteResults { class CodeCompleteResults {
@ -11,6 +11,7 @@ namespace clangmm {
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer, CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer,
unsigned line_num, unsigned column); unsigned line_num, unsigned column);
public: public:
CodeCompleteResults(CodeCompleteResults &) = delete; CodeCompleteResults(CodeCompleteResults &) = delete;
CodeCompleteResults(CodeCompleteResults &&rhs); CodeCompleteResults(CodeCompleteResults &&rhs);

3
src/compilation_database.h

@ -7,6 +7,7 @@
namespace clangmm { namespace clangmm {
class CompilationDatabase { class CompilationDatabase {
CXCompilationDatabase_Error cx_db_error; CXCompilationDatabase_Error cx_db_error;
public: public:
explicit CompilationDatabase(const std::string &project_path); explicit CompilationDatabase(const std::string &project_path);
~CompilationDatabase(); ~CompilationDatabase();
@ -15,6 +16,6 @@ namespace clangmm {
CXCompilationDatabase cx_db; CXCompilationDatabase cx_db;
}; };
} } // namespace clangmm
#endif // COMPILATIONDATABASE_H_ #endif // COMPILATIONDATABASE_H_

4
src/compile_command.h

@ -1,8 +1,8 @@
#ifndef COMPILECOMMAND_H_ #ifndef COMPILECOMMAND_H_
#define COMPILECOMMAND_H_ #define COMPILECOMMAND_H_
#include <clang-c/CXCompilationDatabase.h> #include <clang-c/CXCompilationDatabase.h>
#include <vector>
#include <string> #include <string>
#include <vector>
namespace clangmm { namespace clangmm {
class CompileCommand { class CompileCommand {
@ -12,5 +12,5 @@ namespace clangmm {
CXCompileCommand cx_command; CXCompileCommand cx_command;
}; };
} } // namespace clangmm
#endif // COMPILECOMMAND_H_ #endif // COMPILECOMMAND_H_

2
src/compile_commands.h

@ -15,5 +15,5 @@ namespace clangmm {
CXCompileCommands cx_commands; CXCompileCommands cx_commands;
}; };
} } // namespace clangmm
#endif // COMPILECOMMANDS_H_ #endif // COMPILECOMMANDS_H_

2
src/completion_string.h

@ -1,9 +1,9 @@
#ifndef COMPLETIONSTRING_H_ #ifndef COMPLETIONSTRING_H_
#define COMPLETIONSTRING_H_ #define COMPLETIONSTRING_H_
#include "cursor.h"
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include "cursor.h"
namespace clangmm { namespace clangmm {
enum CompletionChunkKind { enum CompletionChunkKind {

7
src/cursor.cc

@ -159,13 +159,10 @@ clangmm::Cursor clangmm::Cursor::get_semantic_parent() const {
std::vector<clangmm::Cursor> clangmm::Cursor::get_children() const { std::vector<clangmm::Cursor> clangmm::Cursor::get_children() const {
std::vector<Cursor> result; std::vector<Cursor> result;
clang_visitChildren(cx_cursor, clang_visitChildren(cx_cursor, [](CXCursor cur, CXCursor /*parent*/, CXClientData data) {
[](CXCursor cur, CXCursor /*parent*/, CXClientData data) {
static_cast<std::vector<Cursor> *>(data)->emplace_back(cur); static_cast<std::vector<Cursor> *>(data)->emplace_back(cur);
return CXChildVisit_Continue; return CXChildVisit_Continue;
}, }, &result);
&result
);
return result; return result;
} }

4
src/cursor.h

@ -1,11 +1,11 @@
#ifndef CURSOR_H_ #ifndef CURSOR_H_
#define CURSOR_H_ #define CURSOR_H_
#include <clang-c/Index.h>
#include "source_location.h" #include "source_location.h"
#include "source_range.h" #include "source_range.h"
#include <clang-c/Index.h>
#include <string> #include <string>
#include <vector>
#include <unordered_set> #include <unordered_set>
#include <vector>
namespace clangmm { namespace clangmm {
class Cursor { class Cursor {

11
src/diagnostic.h

@ -1,14 +1,15 @@
#ifndef DIAGNOSTIC_H_ #ifndef DIAGNOSTIC_H_
#define DIAGNOSTIC_H_ #define DIAGNOSTIC_H_
#include "source_range.h"
#include <clang-c/Index.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <clang-c/Index.h>
#include "source_range.h"
namespace clangmm { namespace clangmm {
class Diagnostic { class Diagnostic {
friend class TranslationUnit; friend class TranslationUnit;
Diagnostic(CXTranslationUnit &cx_tu, CXDiagnostic &cx_diagnostic); Diagnostic(CXTranslationUnit &cx_tu, CXDiagnostic &cx_diagnostic);
public: public:
enum class Severity { enum class Severity {
Ignored = 0, Ignored = 0,
@ -20,8 +21,8 @@ namespace clangmm {
class FixIt { class FixIt {
public: public:
FixIt(const std::string &source, const std::pair<clangmm::Offset, clangmm::Offset> &offsets): FixIt(const std::string &source, const std::pair<clangmm::Offset, clangmm::Offset> &offsets)
source(source), offsets(offsets) {} : source(source), offsets(offsets) {}
std::string source; std::string source;
std::pair<clangmm::Offset, clangmm::Offset> offsets; std::pair<clangmm::Offset, clangmm::Offset> offsets;
}; };
@ -32,6 +33,6 @@ namespace clangmm {
std::pair<clangmm::Offset, clangmm::Offset> offsets; std::pair<clangmm::Offset, clangmm::Offset> offsets;
std::vector<FixIt> fix_its; std::vector<FixIt> fix_its;
}; };
} } // namespace clangmm
#endif // DIAGNOSTIC_H_ #endif // DIAGNOSTIC_H_

1
src/source_location.cc

@ -36,4 +36,3 @@ void clangmm::SourceLocation::get_data(std::string* path, unsigned *line, unsign
} }
} }
} }

3
src/source_location.h

@ -1,8 +1,8 @@
#ifndef SOURCELOCATION_H_ #ifndef SOURCELOCATION_H_
#define SOURCELOCATION_H_ #define SOURCELOCATION_H_
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include <string>
#include <ostream> #include <ostream>
#include <string>
namespace clangmm { namespace clangmm {
class Offset { class Offset {
@ -22,6 +22,7 @@ namespace clangmm {
friend class Diagnostic; friend class Diagnostic;
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset); SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned offset);
SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned line, unsigned column); SourceLocation(CXTranslationUnit &tu, const std::string &filepath, unsigned line, unsigned column);
public: public:
SourceLocation(const CXSourceLocation &cx_location) : cx_location(cx_location) {} SourceLocation(const CXSourceLocation &cx_location) : cx_location(cx_location) {}

2
src/source_range.h

@ -1,7 +1,7 @@
#ifndef SOURCERANGE_H_ #ifndef SOURCERANGE_H_
#define SOURCERANGE_H_ #define SOURCERANGE_H_
#include <clang-c/Index.h>
#include "source_location.h" #include "source_location.h"
#include <clang-c/Index.h>
#include <string> #include <string>
#include <utility> #include <utility>

11
src/token.h

@ -1,14 +1,15 @@
#ifndef TOKEN_H_ #ifndef TOKEN_H_
#define TOKEN_H_ #define TOKEN_H_
#include <clang-c/Index.h> #include "cursor.h"
#include "source_location.h" #include "source_location.h"
#include "source_range.h" #include "source_range.h"
#include "cursor.h" #include <clang-c/Index.h>
#include <string> #include <string>
namespace clangmm { namespace clangmm {
class Token { class Token {
friend class Tokens; friend class Tokens;
public: public:
enum Kind { enum Kind {
Punctuation, Punctuation,
@ -17,9 +18,11 @@ namespace clangmm {
Literal, Literal,
Comment Comment
}; };
private: private:
Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor): Token(CXTranslationUnit &cx_tu, CXToken &cx_token, CXCursor &cx_cursor)
cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor) {} : cx_tu(cx_tu), cx_token(cx_token), cx_cursor(cx_cursor) {}
public: public:
Kind get_kind() const; Kind get_kind() const;
std::string get_spelling() const; std::string get_spelling() const;

2
src/tokens.cc

@ -1,8 +1,8 @@
#include "tokens.h" #include "tokens.h"
#include "utility.h" #include "utility.h"
#include <unordered_set>
#include <cstring> #include <cstring>
#include <map> #include <map>
#include <unordered_set>
clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool annotate_tokens) : cx_tu(cx_tu) { clangmm::Tokens::Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool annotate_tokens) : cx_tu(cx_tu) {
unsigned num_tokens; unsigned num_tokens;

6
src/tokens.h

@ -1,21 +1,23 @@
#ifndef TOKENS_H_ #ifndef TOKENS_H_
#define TOKENS_H_ #define TOKENS_H_
#include <clang-c/Index.h>
#include "source_range.h" #include "source_range.h"
#include "token.h" #include "token.h"
#include <clang-c/Index.h>
#include <memory>
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#include <memory>
namespace clangmm { namespace clangmm {
class Tokens : public std::vector<clangmm::Token> { class Tokens : public std::vector<clangmm::Token> {
friend class TranslationUnit; friend class TranslationUnit;
friend class Diagnostic; friend class Diagnostic;
Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool annotate_tokens = true); Tokens(CXTranslationUnit &cx_tu, const SourceRange &range, bool annotate_tokens = true);
public: public:
~Tokens(); ~Tokens();
std::vector<std::pair<clangmm::Offset, clangmm::Offset>> get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling, std::vector<std::pair<clangmm::Offset, clangmm::Offset>> get_similar_token_offsets(Cursor::Kind kind, const std::string &spelling,
const std::unordered_set<std::string> &usrs); const std::unordered_set<std::string> &usrs);
private: private:
CXToken *cx_tokens; CXToken *cx_tokens;
std::unique_ptr<CXCursor[]> cx_cursors; std::unique_ptr<CXCursor[]> cx_cursors;

15
src/translation_unit.h

@ -1,15 +1,15 @@
#ifndef TRANSLATIONUNIT_H_ #ifndef TRANSLATIONUNIT_H_
#define TRANSLATIONUNIT_H_ #define TRANSLATIONUNIT_H_
#include "code_complete_results.h"
#include "cursor.h"
#include "diagnostic.h"
#include "index.h"
#include "tokens.h"
#include <clang-c/Index.h> #include <clang-c/Index.h>
#include <string>
#include <vector>
#include <map> #include <map>
#include <memory> #include <memory>
#include "index.h" #include <string>
#include "diagnostic.h" #include <vector>
#include "tokens.h"
#include "code_complete_results.h"
#include "cursor.h"
namespace clangmm { namespace clangmm {
class TranslationUnit { class TranslationUnit {
@ -52,4 +52,3 @@ namespace clangmm {
}; };
} // namespace clangmm } // namespace clangmm
#endif // TRANSLATIONUNIT_H_ #endif // TRANSLATIONUNIT_H_

3
src/utility.cc

@ -55,7 +55,8 @@ void clangmm::remove_include_guard(std::string &buffer) {
} }
std::smatch sm; std::smatch sm;
if(empty_line) {} if(empty_line) {
}
else if(!found_ifndef && (std::regex_match(line, sm, ifndef_regex1) || std::regex_match(line, sm, ifndef_regex2))) { else if(!found_ifndef && (std::regex_match(line, sm, ifndef_regex1) || std::regex_match(line, sm, ifndef_regex2))) {
found_ifndef = true; found_ifndef = true;
ranges.emplace_back(start_of_line, c); ranges.emplace_back(start_of_line, c);

2
src/utility.h

@ -15,6 +15,6 @@ namespace clangmm {
}; };
void remove_include_guard(std::string &buffer); void remove_include_guard(std::string &buffer);
} } // namespace clangmm
#endif // UTILITY_H_ #endif // UTILITY_H_

2
tests/code_complete_results_test.cc

@ -1,7 +1,7 @@
#include "clangmm.h" #include "clangmm.h"
#include <string>
#include <cassert> #include <cassert>
#include <regex> #include <regex>
#include <string>
int main() { int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;

2
tests/completion_string_test.cc

@ -1,7 +1,7 @@
#include "clangmm.h" #include "clangmm.h"
#include <string>
#include <cassert> #include <cassert>
#include <regex> #include <regex>
#include <string>
int main() { int main() {
{ {

2
tests/cursor_test.cc

@ -1,7 +1,7 @@
#include "clangmm.h" #include "clangmm.h"
#include <string>
#include <cassert> #include <cassert>
#include <regex> #include <regex>
#include <string>
int main() { int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;

4
tests/diagnostics_test.cc

@ -1,7 +1,7 @@
#include "clangmm.h" #include "clangmm.h"
#include <iostream>
#include <fstream>
#include <cassert> #include <cassert>
#include <fstream>
#include <iostream>
#include <regex> #include <regex>
using namespace std; using namespace std;

2
tests/source_location_test.cc

@ -1,6 +1,6 @@
#include "clangmm.h" #include "clangmm.h"
#include <string>
#include <cassert> #include <cassert>
#include <string>
int main() { int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;

2
tests/token_test.cc

@ -1,6 +1,6 @@
#include "clangmm.h" #include "clangmm.h"
#include <string>
#include <cassert> #include <cassert>
#include <string>
int main() { int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;

4
tests/translation_unit_test.cc

@ -1,7 +1,7 @@
#include "clangmm.h" #include "clangmm.h"
#include <string>
#include <map>
#include <cassert> #include <cassert>
#include <map>
#include <string>
int main() { int main() {
std::string tests_path = LIBCLANGMM_TESTS_PATH; std::string tests_path = LIBCLANGMM_TESTS_PATH;

Loading…
Cancel
Save