Commit 791b76b0 authored by Ole Christian Eidheim's avatar Ole Christian Eidheim
Browse files

Formatted code with custom clang-format

parent 8e446de1
Pipeline #25351171 passed with stage
in 3 minutes and 9 seconds
IndentWidth: 2
AccessModifierOffset: -2
UseTab: Never
ColumnLimit: 0
MaxEmptyLinesToKeep: 2
SpaceBeforeParens: Never
BreakBeforeBraces: Custom
BraceWrapping: {BeforeElse: true, BeforeCatch: true}
NamespaceIndentation: All
......@@ -13,7 +13,7 @@ Developed for [juCi++](https://gitlab.com/cppit/jucipp), a lightweight, platform
## Installation ##
See [installation guide](https://gitlab.com/cppit/libclangmm/blob/master/docs/install.md)
# Tests #
## Tests ##
To run the unit tests:
```sh
mkdir build && cd build
......@@ -21,3 +21,31 @@ cmake -DBUILD_TESTING=1 ..
make
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))
```
#ifndef CLANGMM_H_
#define CLANGMM_H_
#include "translation_unit.h"
#include "source_location.h"
#include "source_range.h"
#include "token.h"
#include "tokens.h"
#include "code_complete_results.h"
#include "compilation_database.h"
#include "compile_commands.h"
#include "compile_command.h"
#include "code_complete_results.h"
#include "compile_commands.h"
#include "completion_string.h"
#include "index.h"
#include "cursor.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"
#endif // CLANGMM_H_
#endif // CLANGMM_H_
......@@ -2,22 +2,22 @@
#include "completion_string.h"
#include "utility.h"
clangmm::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
const std::string &buffer,
unsigned line_num, unsigned column) {
clangmm::CodeCompleteResults::CodeCompleteResults(CXTranslationUnit &cx_tu,
const std::string &buffer,
unsigned line_num, unsigned column) {
CXUnsavedFile files[1];
auto file_path=to_string(clang_getTranslationUnitSpelling(cx_tu));
auto file_path = to_string(clang_getTranslationUnitSpelling(cx_tu));
files[0].Filename = file_path.c_str();
files[0].Contents = buffer.c_str();
files[0].Length = buffer.size();
cx_results = clang_codeCompleteAt(cx_tu,
file_path.c_str(),
line_num,
column,
files,
1,
clang_defaultCodeCompleteOptions()|CXCodeComplete_IncludeBriefComments);
file_path.c_str(),
line_num,
column,
files,
1,
clang_defaultCodeCompleteOptions() | CXCodeComplete_IncludeBriefComments);
if(cx_results)
clang_sortCodeCompletionResults(cx_results->Results, cx_results->NumResults);
}
......@@ -27,11 +27,11 @@ clangmm::CodeCompleteResults::CodeCompleteResults(CodeCompleteResults &&rhs) : c
}
clangmm::CodeCompleteResults &clangmm::CodeCompleteResults::operator=(CodeCompleteResults &&rhs) {
if(this!=&rhs) {
if(this != &rhs) {
if(cx_results)
clang_disposeCodeCompleteResults(cx_results);
cx_results=rhs.cx_results;
rhs.cx_results=nullptr;
cx_results = rhs.cx_results;
rhs.cx_results = nullptr;
}
return *this;
}
......
#ifndef CODECOMPLETERESULTS_H_
#define CODECOMPLETERESULTS_H_
#include "completion_string.h"
#include <clang-c/Index.h>
#include <map>
#include <string>
#include "completion_string.h"
namespace clangmm {
class CodeCompleteResults {
friend class TranslationUnit;
CodeCompleteResults(CXTranslationUnit &cx_tu, const std::string &buffer,
unsigned line_num, unsigned column);
public:
CodeCompleteResults(CodeCompleteResults &) = delete;
CodeCompleteResults(CodeCompleteResults &&rhs);
......@@ -23,5 +24,5 @@ namespace clangmm {
CXCodeCompleteResults *cx_results;
};
} // namespace clangmm
#endif // CODECOMPLETERESULTS_H_
} // namespace clangmm
#endif // CODECOMPLETERESULTS_H_
......@@ -7,14 +7,15 @@
namespace clangmm {
class CompilationDatabase {
CXCompilationDatabase_Error cx_db_error;
public:
explicit CompilationDatabase(const std::string &project_path);
~CompilationDatabase();
operator bool() const;
CXCompilationDatabase cx_db;
};
}
} // namespace clangmm
#endif // COMPILATIONDATABASE_H_
#endif // COMPILATIONDATABASE_H_
......@@ -5,7 +5,7 @@
std::vector<std::string> clangmm::CompileCommand::get_arguments() {
unsigned size = clang_CompileCommand_getNumArgs(cx_command);
std::vector<std::string> arguments;
for (unsigned i = 0; i < size; i++)
for(unsigned i = 0; i < size; i++)
arguments.emplace_back(to_string(clang_CompileCommand_getArg(cx_command, i)));
return arguments;
}
#ifndef COMPILECOMMAND_H_
#define COMPILECOMMAND_H_
#include <clang-c/CXCompilationDatabase.h>
#include <vector>
#include <string>
#include <vector>
namespace clangmm {
class CompileCommand {
public:
CompileCommand(const CXCompileCommand& cx_command) : cx_command(cx_command) {};
CompileCommand(const CXCompileCommand &cx_command) : cx_command(cx_command){};
std::vector<std::string> get_arguments();
CXCompileCommand cx_command;
};
}
#endif // COMPILECOMMAND_H_
} // namespace clangmm
#endif // COMPILECOMMAND_H_
......@@ -3,7 +3,7 @@
clangmm::CompileCommands::CompileCommands(const std::string &filename, CompilationDatabase &db) {
if(!filename.empty())
cx_commands = clang_CompilationDatabase_getCompileCommands(db.cx_db, filename.c_str());
if(filename.empty() || clang_CompileCommands_getSize(cx_commands)==0)
if(filename.empty() || clang_CompileCommands_getSize(cx_commands) == 0)
cx_commands = clang_CompilationDatabase_getAllCompileCommands(db.cx_db);
}
......@@ -15,7 +15,7 @@ std::vector<clangmm::CompileCommand> clangmm::CompileCommands::get_commands() {
unsigned size = clang_CompileCommands_getSize(cx_commands);
std::vector<CompileCommand> commands;
commands.reserve(size);
for (unsigned i = 0; i < size; i++)
for(unsigned i = 0; i < size; i++)
commands.emplace_back(clang_CompileCommands_getCommand(cx_commands, i));
return commands;
}
......@@ -15,5 +15,5 @@ namespace clangmm {
CXCompileCommands cx_commands;
};
}
#endif // COMPILECOMMANDS_H_
} // namespace clangmm
#endif // COMPILECOMMANDS_H_
......@@ -39,96 +39,96 @@ clangmm::Cursor clangmm::CompletionString::get_cursor(CXTranslationUnit &tu) con
public:
static void remove_template_argument_and_namespace(std::string &chunk) {
size_t pos1, pos2;
if((pos1=chunk.find('<'))!=std::string::npos && (pos2=chunk.rfind('>'))!=std::string::npos)
chunk=chunk.substr(0, pos1)+chunk.substr(pos2+1);
if((pos2=chunk.rfind("::"))!=std::string::npos) {
pos1=pos2-1;
while(pos1!=std::string::npos && ((chunk[pos1]>='a' && chunk[pos1]<='z') || (chunk[pos1]>='A' && chunk[pos1]<='Z') ||
(chunk[pos1]>='0' && chunk[pos1]<='9') || chunk[pos1]==':' || chunk[pos1]=='_'))
if((pos1 = chunk.find('<')) != std::string::npos && (pos2 = chunk.rfind('>')) != std::string::npos)
chunk = chunk.substr(0, pos1) + chunk.substr(pos2 + 1);
if((pos2 = chunk.rfind("::")) != std::string::npos) {
pos1 = pos2 - 1;
while(pos1 != std::string::npos && ((chunk[pos1] >= 'a' && chunk[pos1] <= 'z') || (chunk[pos1] >= 'A' && chunk[pos1] <= 'Z') ||
(chunk[pos1] >= '0' && chunk[pos1] <= '9') || chunk[pos1] == ':' || chunk[pos1] == '_'))
--pos1;
chunk=chunk.substr(0, pos1+1)+chunk.substr(pos2+2);
chunk = chunk.substr(0, pos1 + 1) + chunk.substr(pos2 + 2);
}
}
};
std::vector<std::string> chunks;
for(unsigned i=0;i<clang_getNumCompletionChunks(cx_completion_string);++i) {
for(unsigned i = 0; i < clang_getNumCompletionChunks(cx_completion_string); ++i) {
auto kind = clang_getCompletionChunkKind(cx_completion_string, i);
if(kind != CXCompletionChunk_Optional && kind != CXCompletionChunk_Informative) {
auto chunk=clangmm::to_string(clang_getCompletionChunkText(cx_completion_string, i));
auto chunk = clangmm::to_string(clang_getCompletionChunkText(cx_completion_string, i));
ChunkString::remove_template_argument_and_namespace(chunk);
chunks.emplace_back(chunk);
}
}
auto parent=clangmm::to_string(clang_getCompletionParent(cx_completion_string, nullptr));
auto parent = clangmm::to_string(clang_getCompletionParent(cx_completion_string, nullptr));
std::vector<std::string> parent_parts;
if(!parent.empty()) {
size_t pos=0;
size_t last_pos=0;
while((pos=parent.find("::", pos))!=std::string::npos) {
parent_parts.emplace_back(parent.substr(last_pos, pos-last_pos));
pos+=2;
last_pos=pos;
size_t pos = 0;
size_t last_pos = 0;
while((pos = parent.find("::", pos)) != std::string::npos) {
parent_parts.emplace_back(parent.substr(last_pos, pos - last_pos));
pos += 2;
last_pos = pos;
}
parent_parts.emplace_back(parent.substr(last_pos));
}
VisitorData visitor_data{chunks, parent_parts, clangmm::Cursor()};
clang_visitChildren(clang_getTranslationUnitCursor(tu), [](CXCursor cx_cursor, CXCursor cx_parent, CXClientData cx_data) {
auto data = static_cast<VisitorData *>(cx_data);
bool equal=true;
auto cx_tmp_cursor=cx_parent;
if(clang_getCursorKind(cx_tmp_cursor)!=CXCursorKind::CXCursor_TranslationUnit) {
int c=0;
auto it=data->parent_parts.rbegin();
for(;it!=data->parent_parts.rend();++it) {
auto name=clangmm::to_string(clang_getCursorDisplayName(cx_tmp_cursor));
bool equal = true;
auto cx_tmp_cursor = cx_parent;
if(clang_getCursorKind(cx_tmp_cursor) != CXCursorKind::CXCursor_TranslationUnit) {
int c = 0;
auto it = data->parent_parts.rbegin();
for(; it != data->parent_parts.rend(); ++it) {
auto name = clangmm::to_string(clang_getCursorDisplayName(cx_tmp_cursor));
size_t pos;
if((pos=name.find('<'))!=std::string::npos)
name=name.substr(0, pos);
if(name!=*it) {
equal=false;
if((pos = name.find('<')) != std::string::npos)
name = name.substr(0, pos);
if(name != *it) {
equal = false;
break;
}
cx_tmp_cursor=clang_getCursorSemanticParent(cx_tmp_cursor);
if(clang_getCursorKind(cx_tmp_cursor)==CXCursorKind::CXCursor_TranslationUnit) {
cx_tmp_cursor = clang_getCursorSemanticParent(cx_tmp_cursor);
if(clang_getCursorKind(cx_tmp_cursor) == CXCursorKind::CXCursor_TranslationUnit) {
++it;
break;
}
++c;
}
if(it!=data->parent_parts.rend())
equal=false;
if(it != data->parent_parts.rend())
equal = false;
}
else if(!data->parent_parts.empty())
return CXChildVisit_Recurse;
if(equal) {
auto completion_string = clang_getCursorCompletionString(cx_cursor);
auto num_completion_chunks=clang_getNumCompletionChunks(completion_string);
if(num_completion_chunks>=data->completion_chunks.size()) {
bool equal=true;
for(unsigned i=0;i<data->completion_chunks.size() && i<num_completion_chunks;++i) {
auto num_completion_chunks = clang_getNumCompletionChunks(completion_string);
if(num_completion_chunks >= data->completion_chunks.size()) {
bool equal = true;
for(unsigned i = 0; i < data->completion_chunks.size() && i < num_completion_chunks; ++i) {
auto kind = clang_getCompletionChunkKind(completion_string, i);
if(kind != CXCompletionChunk_Optional && kind != CXCompletionChunk_Informative) {
auto chunk=clangmm::to_string(clang_getCompletionChunkText(completion_string, i));
auto chunk = clangmm::to_string(clang_getCompletionChunkText(completion_string, i));
ChunkString::remove_template_argument_and_namespace(chunk);
if(data->completion_chunks[i]!=chunk) {
equal=false;
if(data->completion_chunks[i] != chunk) {
equal = false;
break;
}
}
}
if(equal) {
data->found_cursor=cx_cursor;
data->found_cursor = cx_cursor;
return CXChildVisit_Break;
}
}
}
return CXChildVisit_Recurse;
}, &visitor_data);
return Cursor(visitor_data.found_cursor);
}
#ifndef COMPLETIONSTRING_H_
#define COMPLETIONSTRING_H_
#include "cursor.h"
#include <clang-c/Index.h>
#include <string>
#include <vector>
#include "cursor.h"
namespace clangmm {
enum CompletionChunkKind {
CompletionChunk_Optional, CompletionChunk_TypedText,
CompletionChunk_Optional, CompletionChunk_TypedText,
CompletionChunk_Text, CompletionChunk_Placeholder,
CompletionChunk_Informative, CompletionChunk_CurrentParameter,
CompletionChunk_LeftParen, CompletionChunk_RightParen,
......@@ -34,11 +34,11 @@ namespace clangmm {
std::vector<CompletionChunk> get_chunks() const;
unsigned get_num_chunks() const;
std::string get_brief_comment() const;
/// Search for the corresponding cursor
Cursor get_cursor(CXTranslationUnit &tu) const;
CXCompletionString cx_completion_string;
};
} // namespace clangmm
#endif // COMPLETIONSTRING_H_
} // namespace clangmm
#endif // COMPLETIONSTRING_H_
......@@ -14,7 +14,7 @@ clangmm::Cursor clangmm::Cursor::Type::get_cursor() const {
return Cursor(clang_getTypeDeclaration(cx_type));
}
bool clangmm::Cursor::Type::operator==(const Cursor::Type& rhs) const {
bool clangmm::Cursor::Type::operator==(const Cursor::Type &rhs) const {
return clang_equalTypes(cx_type, rhs.cx_type);
}
......@@ -27,24 +27,24 @@ std::string clangmm::Cursor::get_kind_spelling() const {
}
bool clangmm::Cursor::is_similar_kind(Kind kind, Kind other_kind) {
auto is_function_or_method=[](Kind kind) {
if(kind==Kind::FunctionDecl || kind==Kind::CXXMethod || kind==Kind::FunctionTemplate)
auto is_function_or_method = [](Kind kind) {
if(kind == Kind::FunctionDecl || kind == Kind::CXXMethod || kind == Kind::FunctionTemplate)
return true;
return false;
};
auto is_class_or_struct=[](Kind kind) {
if(kind==Kind::ClassDecl || kind==Kind::StructDecl || kind==Kind::ClassTemplate ||
kind==Cursor::Kind::Constructor || kind==Cursor::Kind::Destructor || kind==Cursor::Kind::FunctionTemplate)
auto is_class_or_struct = [](Kind kind) {
if(kind == Kind::ClassDecl || kind == Kind::StructDecl || kind == Kind::ClassTemplate ||
kind == Cursor::Kind::Constructor || kind == Cursor::Kind::Destructor || kind == Cursor::Kind::FunctionTemplate)
return true;
return false;
};
if(kind==Kind::FunctionTemplate)
if(kind == Kind::FunctionTemplate)
return is_function_or_method(other_kind) || is_class_or_struct(other_kind);
if(is_function_or_method(kind))
return is_function_or_method(other_kind);
if(is_class_or_struct(kind))
return is_class_or_struct(other_kind);
return kind==other_kind;
return kind == other_kind;
}
clangmm::Cursor::Type clangmm::Cursor::get_type() const {
......@@ -68,15 +68,15 @@ std::string clangmm::Cursor::get_display_name() const {
}
std::string clangmm::Cursor::get_token_spelling() const {
auto spelling=get_spelling();
for(size_t i=0;i<spelling.size();++i) {
if(spelling[i]=='<' || spelling[i]=='(') {
if(i>0 && spelling[0]=='~')
return spelling.substr(1, i-1);
auto spelling = get_spelling();
for(size_t i = 0; i < spelling.size(); ++i) {
if(spelling[i] == '<' || spelling[i] == '(') {
if(i > 0 && spelling[0] == '~')
return spelling.substr(1, i - 1);
return spelling.substr(0, i);
}
}
if(!spelling.empty() && spelling[0]=='~')
if(!spelling.empty() && spelling[0] == '~')
return spelling.substr(1);
return spelling;
}
......@@ -88,38 +88,38 @@ std::string clangmm::Cursor::get_usr() const {
std::string clangmm::Cursor::get_usr_extended() const {
if(!is_valid_kind())
return std::string();
auto cursor=*this;
auto kind=cursor.get_kind();
auto cursor = *this;
auto kind = cursor.get_kind();
// If constructor, destructor or function template, and the token spelling is equal, set cursor to parent
if(kind==Cursor::Kind::Constructor || kind==Cursor::Kind::Destructor ||
kind==Cursor::Kind::FunctionTemplate) {
auto parent=cursor.get_semantic_parent();
auto parent_kind=parent.get_kind();
if((parent_kind==Cursor::Kind::ClassDecl || parent_kind==Cursor::Kind::StructDecl || parent_kind==Cursor::Kind::ClassTemplate) &&
cursor.get_token_spelling()==parent.get_token_spelling())
cursor=parent;
if(kind == Cursor::Kind::Constructor || kind == Cursor::Kind::Destructor ||
kind == Cursor::Kind::FunctionTemplate) {
auto parent = cursor.get_semantic_parent();
auto parent_kind = parent.get_kind();
if((parent_kind == Cursor::Kind::ClassDecl || parent_kind == Cursor::Kind::StructDecl || parent_kind == Cursor::Kind::ClassTemplate) &&
cursor.get_token_spelling() == parent.get_token_spelling())
cursor = parent;
}
std::string usr=cursor.get_token_spelling();
auto parent=cursor.get_semantic_parent();
while((kind=parent.get_kind())!=Kind::TranslationUnit && parent.is_valid_kind()) {
if(kind==Kind::CXXMethod || kind==Kind::FunctionDecl || kind==Kind::FunctionTemplate ||
kind==Kind::Constructor || kind==Kind::Destructor) {
auto canonical=cursor.get_canonical();
auto location=canonical.get_source_location();
auto offset=location.get_offset();
return std::to_string(offset.line)+':'+std::to_string(offset.index)+':'+location.get_path();
std::string usr = cursor.get_token_spelling();
auto parent = cursor.get_semantic_parent();
while((kind = parent.get_kind()) != Kind::TranslationUnit && parent.is_valid_kind()) {
if(kind == Kind::CXXMethod || kind == Kind::FunctionDecl || kind == Kind::FunctionTemplate ||
kind == Kind::Constructor || kind == Kind::Destructor) {
auto canonical = cursor.get_canonical();
auto location = canonical.get_source_location();
auto offset = location.get_offset();
return std::to_string(offset.line) + ':' + std::to_string(offset.index) + ':' + location.get_path();
}
usr+=':'+parent.get_token_spelling();
parent=parent.get_semantic_parent();
usr += ':' + parent.get_token_spelling();
parent = parent.get_semantic_parent();
}
return usr;
}
std::unordered_set<std::string> clangmm::Cursor::get_all_usr_extended() const {
std::unordered_set<std::string> usrs;
if(get_kind()==Kind::CXXMethod) {
if(get_kind() == Kind::CXXMethod) {
class Recursive {
public:
static void overridden(std::unordered_set<std::string> &usrs, const Cursor &cursor) {
......@@ -127,7 +127,7 @@ std::unordered_set<std::string> clangmm::Cursor::get_all_usr_extended() const {
CXCursor *cursors;
unsigned size;
clang_getOverriddenCursors(cursor.cx_cursor, &cursors, &size);
for(unsigned c=0;c<size;++c)
for(unsigned c = 0; c < size; ++c)
overridden(usrs, cursors[c]);
clang_disposeOverriddenCursors(cursors);
}
......@@ -159,39 +159,36 @@ clangmm::Cursor clangmm::Cursor::get_semantic_parent() const {
std::vector<clangmm::Cursor> clangmm::Cursor::get_children() const {
std::vector<Cursor> result;
clang_visitChildren(cx_cursor,
[](CXCursor cur, CXCursor /*parent*/, CXClientData data) {
static_cast<std::vector<Cursor>*>(data)->emplace_back(cur);
return CXChildVisit_Continue;
},
&result
);
clang_visitChildren(cx_cursor, [](CXCursor cur, CXCursor /*parent*/, CXClientData data) {
static_cast<std::vector<Cursor> *>(data)->emplace_back(cur);
return CXChildVisit_Continue;
}, &result);
return result;
}
std::vector<clangmm::Cursor> clangmm::Cursor::get_arguments() const {
std::vector<Cursor> cursors;
auto size=clang_Cursor_getNumArguments(cx_cursor);
for(int c=0;c<size;++c)
auto size = clang_Cursor_getNumArguments(cx_cursor);
for(int c = 0; c < size; ++c)
cursors.emplace_back(clang_Cursor_getArgument(cx_cursor, c));
return cursors;
}
std::vector<clangmm::Cursor> clangmm::Cursor::get_all_overridden_cursors() const {
std::vector<Cursor> result;
if(get_kind()!=Kind::CXXMethod)
if(get_kind() != Kind::CXXMethod)
return result;
class Recursive {