Commit a3e1c143 authored by Ramin Halavati's avatar Ramin Halavati Committed by Commit Bot

Update traffic_annotation_auditor to accept absolute files.

The latest update of traffic_annotation_extractor clang tool returns
absolute file paths in the reports instead of relative ones, and this
breaks safe list checking.
traffic_annotation_auditor is updated to convert the received file
paths from absolute to relative (based on source folder) so that safe
list checking would run.

Bug: 690323
Change-Id: Iea610f6e3371923f4d1d9c19bd030cf859b8ac44
Reviewed-on: https://chromium-review.googlesource.com/1049547
Commit-Queue: Ramin Halavati <rhalavati@chromium.org>
Reviewed-by: default avatarGeorges Khalil <georgesak@chromium.org>
Cr-Commit-Position: refs/heads/master@{#557099}
parent e2f2e789
...@@ -67,6 +67,7 @@ class AuditorResult { ...@@ -67,6 +67,7 @@ class AuditorResult {
Type type() const { return type_; }; Type type() const { return type_; };
std::string file_path() const { return file_path_; } std::string file_path() const { return file_path_; }
void set_file_path(const std::string& file_path) { file_path_ = file_path; }
// Formats the error message into one line of text. // Formats the error message into one line of text.
std::string ToText() const; std::string ToText() const;
......
...@@ -100,6 +100,32 @@ bool PathFiltersMatch(const std::vector<std::string>& path_filters, ...@@ -100,6 +100,32 @@ bool PathFiltersMatch(const std::vector<std::string>& path_filters,
return false; return false;
} }
// If normalized |file_path| starts with |base_directory|, returns the
// relative path to |file_path|, otherwise the original |file_path| is returned.
std::string MakeRelativePath(const base::FilePath& base_directory,
const std::string& file_path) {
DCHECK(base_directory.IsAbsolute());
#if defined(OS_WIN)
base::FilePath converted_file_path = base::FilePath(
base::FilePath::StringPieceType((base::UTF8ToWide(file_path))));
#else
base::FilePath converted_file_path(file_path);
#endif
base::FilePath normalized_path;
if (base::NormalizeFilePath(converted_file_path, &normalized_path) &&
normalized_path.IsAbsolute()) {
normalized_path = normalized_path.NormalizePathSeparatorsTo('/');
std::string file_str = normalized_path.MaybeAsASCII();
std::string base_str = base_directory.MaybeAsASCII();
if (file_str.find(base_str) == 0) {
return file_str.substr(base_str.length() + 1,
file_str.length() - base_str.length() - 1);
}
}
return file_path;
}
} // namespace } // namespace
TrafficAnnotationAuditor::TrafficAnnotationAuditor( TrafficAnnotationAuditor::TrafficAnnotationAuditor(
...@@ -115,6 +141,15 @@ TrafficAnnotationAuditor::TrafficAnnotationAuditor( ...@@ -115,6 +141,15 @@ TrafficAnnotationAuditor::TrafficAnnotationAuditor(
DCHECK(!build_path.empty()); DCHECK(!build_path.empty());
DCHECK(!clang_tool_path.empty()); DCHECK(!clang_tool_path.empty());
// Get absolute source path.
base::FilePath original_path;
base::GetCurrentDirectory(&original_path);
base::SetCurrentDirectory(source_path_);
base::GetCurrentDirectory(&absolute_source_path_);
base::SetCurrentDirectory(original_path);
absolute_source_path_ = absolute_source_path_.NormalizePathSeparatorsTo('/');
DCHECK(absolute_source_path_.IsAbsolute());
base::FilePath switches_file = base::FilePath switches_file =
base::MakeAbsoluteFilePath(source_path_.Append(kClangToolSwitchesPath)); base::MakeAbsoluteFilePath(source_path_.Append(kClangToolSwitchesPath));
std::string file_content; std::string file_content;
...@@ -406,12 +441,16 @@ bool TrafficAnnotationAuditor::ParseClangToolRawOutput() { ...@@ -406,12 +441,16 @@ bool TrafficAnnotationAuditor::ParseClangToolRawOutput() {
if (block_type == "ANNOTATION") { if (block_type == "ANNOTATION") {
AnnotationInstance new_annotation; AnnotationInstance new_annotation;
result = new_annotation.Deserialize(lines, current, end_line); result = new_annotation.Deserialize(lines, current, end_line);
if (IsSafeListed(new_annotation.proto.source().file(), result.set_file_path(
MakeRelativePath(absolute_source_path_, result.file_path()));
if (IsSafeListed(result.file_path(),
AuditorException::ExceptionType::ALL)) { AuditorException::ExceptionType::ALL)) {
result = AuditorResult(AuditorResult::Type::RESULT_IGNORE); result = AuditorResult(AuditorResult::Type::RESULT_IGNORE);
} }
switch (result.type()) { switch (result.type()) {
case AuditorResult::Type::RESULT_OK: case AuditorResult::Type::RESULT_OK:
new_annotation.proto.mutable_source()->set_file(MakeRelativePath(
absolute_source_path_, new_annotation.proto.source().file()));
extracted_annotations_.push_back(new_annotation); extracted_annotations_.push_back(new_annotation);
break; break;
case AuditorResult::Type::ERROR_MISSING_TAG_USED: case AuditorResult::Type::ERROR_MISSING_TAG_USED:
...@@ -432,6 +471,8 @@ bool TrafficAnnotationAuditor::ParseClangToolRawOutput() { ...@@ -432,6 +471,8 @@ bool TrafficAnnotationAuditor::ParseClangToolRawOutput() {
} else if (block_type == "CALL") { } else if (block_type == "CALL") {
CallInstance new_call; CallInstance new_call;
result = new_call.Deserialize(lines, current, end_line); result = new_call.Deserialize(lines, current, end_line);
new_call.file_path =
MakeRelativePath(absolute_source_path_, new_call.file_path);
if (IsSafeListed(new_call.file_path, if (IsSafeListed(new_call.file_path,
AuditorException::ExceptionType::ALL)) { AuditorException::ExceptionType::ALL)) {
result = AuditorResult(AuditorResult::Type::RESULT_IGNORE); result = AuditorResult(AuditorResult::Type::RESULT_IGNORE);
...@@ -441,6 +482,8 @@ bool TrafficAnnotationAuditor::ParseClangToolRawOutput() { ...@@ -441,6 +482,8 @@ bool TrafficAnnotationAuditor::ParseClangToolRawOutput() {
} else if (block_type == "ASSIGNMENT") { } else if (block_type == "ASSIGNMENT") {
AssignmentInstance new_assignment; AssignmentInstance new_assignment;
result = new_assignment.Deserialize(lines, current, end_line); result = new_assignment.Deserialize(lines, current, end_line);
new_assignment.file_path =
MakeRelativePath(absolute_source_path_, new_assignment.file_path);
if (IsSafeListed(new_assignment.file_path, if (IsSafeListed(new_assignment.file_path,
AuditorException::ExceptionType::ALL)) { AuditorException::ExceptionType::ALL)) {
result = AuditorResult(AuditorResult::Type::RESULT_IGNORE); result = AuditorResult(AuditorResult::Type::RESULT_IGNORE);
......
...@@ -162,6 +162,8 @@ class TrafficAnnotationAuditor { ...@@ -162,6 +162,8 @@ class TrafficAnnotationAuditor {
const base::FilePath build_path_; const base::FilePath build_path_;
const base::FilePath clang_tool_path_; const base::FilePath clang_tool_path_;
base::FilePath absolute_source_path_;
std::vector<std::string> clang_tool_switches_; std::vector<std::string> clang_tool_switches_;
TrafficAnnotationExporter exporter_; TrafficAnnotationExporter exporter_;
......
...@@ -349,16 +349,6 @@ int main(int argc, char* argv[]) { ...@@ -349,16 +349,6 @@ int main(int argc, char* argv[]) {
if (tool_path.empty()) if (tool_path.empty())
tool_path = command_line.GetProgram().DirName(); tool_path = command_line.GetProgram().DirName();
// If source path is not provided, guess it using build path or current
// directory.
if (source_path.empty()) {
if (build_path.empty())
base::GetCurrentDirectory(&source_path);
else
source_path = build_path.Append(base::FilePath::kParentDirectory)
.Append(base::FilePath::kParentDirectory);
}
// Get build directory, if it is empty issue an error. // Get build directory, if it is empty issue an error.
if (build_path.empty()) { if (build_path.empty()) {
LOG(ERROR) LOG(ERROR)
...@@ -368,6 +358,12 @@ int main(int argc, char* argv[]) { ...@@ -368,6 +358,12 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
// If source path is not provided, guess it using build path.
if (source_path.empty()) {
source_path = build_path.Append(base::FilePath::kParentDirectory)
.Append(base::FilePath::kParentDirectory);
}
TrafficAnnotationAuditor auditor(source_path, build_path, tool_path); TrafficAnnotationAuditor auditor(source_path, build_path, tool_path);
// Extract annotations. // Extract annotations.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment