diff options
author | Sergio Martins <sergio.martins@kdab.com> | 2019-06-10 15:36:08 +0100 |
---|---|---|
committer | Sergio Martins <sergio.martins@kdab.com> | 2019-06-10 15:36:08 +0100 |
commit | c7bcc9dbce2b72a37f5aff0e22b2bb51b24413ea (patch) | |
tree | 3afdee751e50440206509cef13be7626ae5b113e | |
parent | 3901d72b120a4c1d80a42a22610fdebb35d27c45 (diff) |
Add some sanity checks
-rwxr-xr-x | dev-scripts/miniAstDumper.py | 73 | ||||
-rw-r--r-- | src/MiniAstDumper.cpp | 44 |
2 files changed, 94 insertions, 23 deletions
diff --git a/dev-scripts/miniAstDumper.py b/dev-scripts/miniAstDumper.py index e16acd4e..dee73549 100755 --- a/dev-scripts/miniAstDumper.py +++ b/dev-scripts/miniAstDumper.py @@ -14,31 +14,67 @@ class SourceLocation: def dump(self): print(self.asString()) + def check_sanity(self): + if self.lineNumber < 0 or self.columnNumber < 0: + print("SourceLocation::check_sanity: loc numbers are invalid! " + self.lineNumber + " ; " + self.columnNumber) + + if not self.filename: + print("SourceLocation::check_sanity: loc filename is invalid!") class FunctionCall: def __init__(self): - self.callee_name = "" + self.callee_id = -1 self.loc_start = SourceLocation() + def check_sanity(self): + if self.callee_id == -1: + print("FunctionCall::check_sanity: callee_id is -1!") + self.loc_start.check_sanity() + class CXXMethod: def __init__(self): self.id = 0 self.qualified_name = "" self.method_flags = 0 + def check_sanity(self): + if self.id == -1: + print("CXXMethod::check_sanity: id is -1!") + + if not self.qualified_name: + print("CXXMethod::check_sanity: qualified_name is empty!") + + class CXXClass: def __init__(self): - self.id = 0 + # self.id = -1 self.qualified_name = "" self.methods = [] self.class_flags = 0 + def check_sanity(self): + #if self.id == -1: + # print("CXXClass::check_sanity: id is -1!") + + if not self.qualified_name: + print("CXXClass::check_sanity: qualified_name is empty!") + + for m in self.methods: + m.check_sanity() + + class GlobalAST: def __init__(self): self.cxx_classes = [] self.function_calls = [] + def check_sanity(self): + for c in self.cxx_classes: + c.check_sanity() + for f in self.function_calls: + f.check_sanity() + _globalAST = GlobalAST() def parse_loc(cborLoc, file_map, tu_cwd): @@ -97,31 +133,32 @@ def load_cbor(filename, globalAST): globalAST.cxx_classes.append(cxxclass) elif stuff['type'] == 48: # CallExpr funccall = FunctionCall() - funccall.callee_name = stuff['calleeName'] + funccall.callee_id = stuff['calleeId'] funccall.loc_start = parse_loc(stuff['loc'], file_map, current_tu_cwd) globalAST.function_calls.append(funccall) +#def get_class_by_name(qualified_name): +# result = [] +# for c in _globalAST.cxx_classes: +# if c.qualified_name == qualified_name: +# result.append(c) +# return result -def get_class_by_name(qualified_name): - result = [] - for c in _globalAST.cxx_classes: - if c.qualified_name == qualified_name: - result.append(c) - return result - -def get_calls_by_name(callee_name): - result = [] - for f in _globalAST.function_calls: - if f.callee_name == callee_name: - result.append(f) - return result +#def get_calls_by_name(callee_name): +# result = [] +# for f in _globalAST.function_calls: +#if f.callee_name == callee_name: +# result.append(f) + #return result load_cbor(sys.argv[1], _globalAST) +_globalAST.check_sanity() + #string_class = get_class_by_name("QString")[0] -for f in get_calls_by_name("QObject::connect"): - print(f.loc_start.dump()) +#for f in get_calls_by_name("QObject::connect"): + #print(f.loc_start.dump()) #for f in _globalAST.function_calls: # print(f.callee_name) diff --git a/src/MiniAstDumper.cpp b/src/MiniAstDumper.cpp index a53b5fb7..9c889c73 100644 --- a/src/MiniAstDumper.cpp +++ b/src/MiniAstDumper.cpp @@ -155,6 +155,9 @@ void MiniASTDumperConsumer::dumpCXXMethodDecl(CXXMethodDecl *method, CborEncoder void MiniASTDumperConsumer::dumpCXXRecordDecl(CXXRecordDecl *rec, CborEncoder *encoder) { + if (rec->isUnion()) + return; + CborEncoder recordMap; cborCreateMap(encoder, &recordMap, CborIndefiniteLength); @@ -165,7 +168,8 @@ void MiniASTDumperConsumer::dumpCXXRecordDecl(CXXRecordDecl *rec, CborEncoder *e cborEncodeString(recordMap, rec->getQualifiedNameAsString().c_str()); cborEncodeString(recordMap, "loc"); - dumpLocation(clazy::getLocStart(rec), &recordMap); + const SourceLocation loc = clazy::getLocStart(rec); + dumpLocation(loc, &recordMap); if (clazy::isQObject(rec)) { // TODO: Use flags cborEncodeString(recordMap, "isQObject"); @@ -180,6 +184,11 @@ void MiniASTDumperConsumer::dumpCXXRecordDecl(CXXRecordDecl *rec, CborEncoder *e } cborCloseContainer(&recordMap, &cborMethodList); cborCloseContainer(&m_cborStuffArray, &recordMap); + + // Sanity: + if (rec->getQualifiedNameAsString().empty()) { + llvm::errs() << "Record has no name. loc=" << loc.printToString(m_ci.getSourceManager()) << "\n"; + } } void MiniASTDumperConsumer::dumpCallExpr(CallExpr *callExpr, CborEncoder *encoder) @@ -194,8 +203,8 @@ void MiniASTDumperConsumer::dumpCallExpr(CallExpr *callExpr, CborEncoder *encode cborEncodeString(callMap, "type"); cborEncodeInt(callMap, callExpr->getStmtClass()); - cborEncodeString(callMap, "calleeName"); // TODO: replace with ID - cborEncodeString(callMap, func->getQualifiedNameAsString().c_str()); + cborEncodeString(callMap, "calleeId"); + cborEncodeInt(callMap, int64_t(func)); cborEncodeString(callMap, "loc"); dumpLocation(clazy::getLocStart(callExpr), &callMap); @@ -206,12 +215,37 @@ void MiniASTDumperConsumer::dumpCallExpr(CallExpr *callExpr, CborEncoder *encode void MiniASTDumperConsumer::dumpLocation(SourceLocation loc, CborEncoder *encoder) { CborEncoder locMap; - cborCreateMap(encoder, &locMap, 3); + cborCreateMap(encoder, &locMap, CborIndefiniteLength); auto &sm = m_ci.getSourceManager(); - const FileID fileId = sm.getFileID(loc); + if (loc.isMacroID()) { + /// The place where the macro is defined: + SourceLocation spellingLoc = sm.getSpellingLoc(loc); + const FileID fileId = sm.getFileID(spellingLoc); + m_fileIds[fileId.getHashValue()] = sm.getFilename(spellingLoc).str(); + + cborEncodeString(locMap, "spellingFileId"); + cborEncodeInt(locMap, fileId.getHashValue()); + + cborEncodeString(locMap, "spellingLine"); + cborEncodeInt(locMap, sm.getSpellingLineNumber(loc)); + + cborEncodeString(locMap, "spellingColumn"); + cborEncodeInt(locMap, sm.getSpellingColumnNumber(loc)); + + /// Get the place where the macro is used: + loc = sm.getExpansionLoc(loc); + } + + const FileID fileId = sm.getFileID(loc); m_fileIds[fileId.getHashValue()] = sm.getFilename(loc).str(); + + if (sm.getFilename(loc).empty()) { + // Shouldn't happen + llvm::errs() << "Invalid filename for " << loc.printToString(sm) << "\n"; + } + cborEncodeString(locMap, "fileId"); cborEncodeInt(locMap, fileId.getHashValue()); |