Added better sanitizer and sanitized the source code.

This commit is contained in:
kobalicekp
2014-02-02 21:39:09 +01:00
parent 0e84072227
commit 2aba235875
12 changed files with 167 additions and 50 deletions

View File

@@ -5,8 +5,8 @@
// Zlib - See LICENSE.md file in the package. // Zlib - See LICENSE.md file in the package.
// [Guard] // [Guard]
#ifndef _GENBLEND_H #ifndef _APP_TEST_GENBLEND_H
#define _GENBLEND_H #define _APP_TEST_GENBLEND_H
// [Dependencies] // [Dependencies]
#include <asmjit/asmjit.h> #include <asmjit/asmjit.h>
@@ -175,4 +175,4 @@ static void blend(asmjit::host::Compiler& c) {
} // asmgen namespace } // asmgen namespace
// [Guard] // [Guard]
#endif // _GENBLEND_H #endif // _APP_TEST_GENBLEND_H

View File

@@ -5,8 +5,8 @@
// Zlib - See LICENSE.md file in the package. // Zlib - See LICENSE.md file in the package.
// [Guard] // [Guard]
#ifndef _GENOPCODE_H #ifndef _APP_TEST_GENOPCODE_H
#define _GENOPCODE_H #define _APP_TEST_GENOPCODE_H
// [Dependencies] // [Dependencies]
#include <asmjit/asmjit.h> #include <asmjit/asmjit.h>
@@ -2307,4 +2307,4 @@ static void opcode(asmjit::host::Assembler& a) {
} // asmgen namespace } // asmgen namespace
// [Guard] // [Guard]
#endif // _GENOPCODE_H #endif // _APP_TEST_GENOPCODE_H

View File

@@ -5,8 +5,8 @@
// Zlib - See LICENSE.md file in the package. // Zlib - See LICENSE.md file in the package.
// [Guard] // [Guard]
#ifndef _ASMJIT_BASE_CONTEXT_H #ifndef _ASMJIT_BASE_CONTEXT_P_H
#define _ASMJIT_BASE_CONTEXT_H #define _ASMJIT_BASE_CONTEXT_P_H
// [Dependencies - AsmJit] // [Dependencies - AsmJit]
#include "../base/compiler.h" #include "../base/compiler.h"
@@ -251,4 +251,4 @@ struct BaseContext {
#include "../base/apiend.h" #include "../base/apiend.h"
// [Guard] // [Guard]
#endif // _ASMJIT_BASE_CONTEXT_H #endif // _ASMJIT_BASE_CONTEXT_P_H

View File

@@ -11,10 +11,10 @@
// [Dependencies - AsmJit] // [Dependencies - AsmJit]
#include "../base/assembler.h" #include "../base/assembler.h"
#include "../base/assert.h" #include "../base/assert.h"
#include "../base/defs.h"
#include "../base/vectypes.h"
#include "../base/compiler.h" #include "../base/compiler.h"
#include "../base/defs.h"
#include "../base/intutil.h" #include "../base/intutil.h"
#include "../base/vectypes.h"
// [Api-Begin] // [Api-Begin]
#include "../base/apibegin.h" #include "../base/apibegin.h"

View File

@@ -15,17 +15,19 @@ var filesToSanitize = (function() {
var fullName = path.normalize(path.join(dir, baseName)); var fullName = path.normalize(path.join(dir, baseName));
var stat = fs.lstatSync(fullName); var stat = fs.lstatSync(fullName);
if (stat.isSymbolicLink()) if (stat.isSymbolicLink()) {
continue; continue;
}
if (stat.isDirectory()) { if (stat.isDirectory()) {
listPrivate(subarray, path.join(dir, baseName), displayDir + baseName, accept); subarray = listPrivate(subarray,
path.join(dir, baseName), displayDir ? displayDir + "/" + baseName : baseName, accept);
continue; continue;
} }
if (stat.isFile()) { if (stat.isFile()) {
if (accept(baseName)) if (accept(baseName))
array.push({ name: fullName, display: displayDir + baseName }); array.push({ name: fullName, display: displayDir ? displayDir + "/" + baseName : baseName });
continue; continue;
} }
} }
@@ -38,6 +40,19 @@ var filesToSanitize = (function() {
}; };
})(); })();
/**
* Inject data into string.
*/
var inject = function(s, start, end, what) {
assert(start <= s.length);
assert(end <= s.length);
return s.substr(0, start) + what + s.substr(end);
};
/**
* Is the extension c++ header file?
*/
var isCppHeaderExt = function(ext) { var isCppHeaderExt = function(ext) {
return ext === ".h" || return ext === ".h" ||
ext === ".hh" || ext === ".hh" ||
@@ -45,6 +60,9 @@ var isCppHeaderExt = function(ext) {
ext === ".hxx" ; ext === ".hxx" ;
}; };
/**
* Is the extension c++ source file?
*/
var isCppSourceExt = function(ext) { var isCppSourceExt = function(ext) {
return ext === ".c" || return ext === ".c" ||
ext === ".cc" || ext === ".cc" ||
@@ -66,7 +84,10 @@ var filesToAccept = function(name) {
ext === ".mm" ; ext === ".mm" ;
}; };
var sanitySpaces = function(data) { /**
* Sanity spaces.
*/
var sanitySpaces = function(data, name) {
// Remove carriage return. // Remove carriage return.
data = data.replace(/\r\n/g, "\n"); data = data.replace(/\r\n/g, "\n");
// Remove spaces before the end of the line. // Remove spaces before the end of the line.
@@ -77,12 +98,109 @@ var sanitySpaces = function(data) {
return data; return data;
}; };
var sanityHeaderGuards = function(data) { /**
* Sanity header guards.
*/
var sanityHeaderGuards = (function() {
var parseGuardName = function(data, i) {
var m = data.substr(i).match(/[\w][\d\w]*/);
return m ? m[0] : null;
};
var makeGuardName = function(name) {
// Remove leading '/' or '\'.
if (/^[\\\/]/.test(name))
name = name.substr(1);
return "_" + name.toUpperCase().replace(/[\/\\\.-]/g, "_");
};
var directiveMarks = [
"#ifndef ",
"#endif // ",
"#define "
];
var directiveNames = [
"#ifndef ",
"#endif ",
"#define "
];
return function(data, name) {
var i = 0;
var nl = true;
var guard = "// " + "[Guard]" + "\n";
var nFound = 0;
while (i < data.length) {
if (nl && data.substr(i, guard.length) === guard) {
i += guard.length;
nFound++;
if (i >= data.length)
break;
for (var j = 0; j < directiveMarks.length; ) {
var m = directiveMarks[j];
if (data.substr(i, m.length) === m && data.charAt(i + m.length) === '_') {
i += directiveMarks[j].length;
var oldGuardName = parseGuardName(data, i);
var newGuardName;
if (oldGuardName) {
console.log(oldGuardName);
var startPosition = i;
var endPosition = i + oldGuardName.length;
newGuardName = makeGuardName(name);
if (oldGuardName !== newGuardName) {
console.log(name + ": " + directiveNames[j] + newGuardName);
data = inject(data, startPosition, endPosition, newGuardName);
i += newGuardName.length;
i = data.indexOf('\n', i);
if (i === -1) {
// Terminates the loop.
i = data.length;
j = 9999;
nl = false;
break;
}
else {
i++;
}
}
}
j += 2;
}
// Don't process '#define' directive if previous '#ifndef' wasn't matched.
else {
if (++j == 2)
break;
}
}
}
else {
nl = data.charAt(i) === '\n';
i++;
}
}
if (nFound & 1) {
console.log(name + ": Odd number of guards found: " + nFound);
}
return data; return data;
}; };
})();
var sanityIncludeOrder = function(data, directive) { /**
* Sanity #include order.
*/
var sanityIncludeOrder = function(data, name, directive) {
var i = 0; var i = 0;
var nl = true; var nl = true;
@@ -92,7 +210,7 @@ var sanityIncludeOrder = function(data, directive) {
var replacement; var replacement;
while (i < data.length) { while (i < data.length) {
if (nl && data.indexOf(directive, i) === i) { if (nl && data.substr(i, directive.length) === directive) {
var iLocal = i var iLocal = i
if (startPosition === -1) { if (startPosition === -1) {
@@ -105,7 +223,7 @@ var sanityIncludeOrder = function(data, directive) {
list.push(data.substring(iLocal, i)); list.push(data.substring(iLocal, i));
break; break;
} }
if (data[i] === '\n') { if (data.charAt(i) === '\n') {
list.push(data.substring(iLocal, i)); list.push(data.substring(iLocal, i));
i++; i++;
break; break;
@@ -118,13 +236,10 @@ var sanityIncludeOrder = function(data, directive) {
if (list.length > 1) { if (list.length > 1) {
list.sort(); list.sort();
replacement = list.join("\n"); replacement = list.join("\n") + "\n";
assert(replacement.length == endPosition - startPosition - 1);
data = data.substring(0, startPosition) + assert(replacement.length == endPosition - startPosition);
replacement + data = inject(data, startPosition, endPosition, replacement);
"\n" +
data.substring(endPosition);
} }
startPosition = -1; startPosition = -1;
@@ -135,7 +250,7 @@ var sanityIncludeOrder = function(data, directive) {
i++; i++;
} }
else { else {
nl = data[i] === '\n'; nl = data.charAt(i) === '\n';
i++; i++;
} }
} }
@@ -143,32 +258,34 @@ var sanityIncludeOrder = function(data, directive) {
return data; return data;
}; };
/**
* Sanity the given data of file.
*/
var sanity = function(data, name) { var sanity = function(data, name) {
var ext = path.extname(name).toLowerCase(); var ext = path.extname(name).toLowerCase();
// Sanity spaces. // Sanity spaces.
data = sanitySpaces(data); data = sanitySpaces(data, name);
// Fix C/C++ header guards. // Fix C/C++ header guards and sort '#include' files.
if (isCppHeaderExt(ext)) { if (isCppHeaderExt(ext)) {
data = sanityHeaderGuards(data); data = sanityHeaderGuards(data, name);
} data = sanityIncludeOrder(data, name, "#include");
// Sort #include files.
if (isCppHeaderExt(ext) || isCppSourceExt(ext)) {
data = sanityIncludeOrder(data, "#include");
} }
return data; return data;
}; };
/**
* Entry.
*/
var main = function(dir) { var main = function(dir) {
filesToSanitize(dir, filesToAccept).forEach(function(file) { filesToSanitize(dir, filesToAccept).forEach(function(file) {
var oldData = fs.readFileSync(file.name, "utf8"); var oldData = fs.readFileSync(file.name, "utf8");
var newData = sanity(oldData, file.display); var newData = sanity(oldData, file.display);
if (oldData !== newData) { if (oldData !== newData) {
console.log("Sanitizing: " + file.display); console.log(file.display + ": Writing...");
fs.writeFileSync(file.name, newData, "utf8"); fs.writeFileSync(file.name, newData, "utf8");
} }
}); });